Virtual input code now working

This commit is contained in:
Henrik Rydgård 2012-10-26 18:42:17 +02:00
parent 70e6752884
commit 748bdb3101
6 changed files with 30 additions and 15 deletions

View File

@ -211,6 +211,7 @@ extern "C" void Java_com_turboviking_libnative_NativeRenderer_displayRender(JNIE
UpdateInputState(&input_state);
NativeUpdate(input_state);
NativeRender();
EndInputState(&input_state);
time_update();
} else {
ELOG("Ended up in nativeRender even though app has quit.%s", "");

View File

@ -306,6 +306,9 @@ int main(int argc, char *argv[]) {
UpdateInputState(&input_state);
NativeUpdate(input_state);
NativeRender();
EndInputState(&input_state);
if (framecount % 60 == 0) {
// glsl_refresh(); // auto-reloads modified GLSL shaders once per second.
}

View File

@ -26,7 +26,7 @@ enum {
MAX_VERTS = 15000,
};
DrawBuffer::DrawBuffer() : count_(0) {
DrawBuffer::DrawBuffer() : count_(0), atlas(0) {
verts_ = new Vertex[MAX_VERTS];
fontscalex = 1.0f;
fontscaley = 1.0f;

View File

@ -69,5 +69,8 @@ private:
inline void UpdateInputState(InputState *input) {
input->pad_buttons_down = (input->pad_last_buttons ^ input->pad_buttons) & input->pad_buttons;
input->pad_buttons_up = (input->pad_last_buttons ^ input->pad_buttons) & input->pad_last_buttons;
}
inline void EndInputState(InputState *input) {
input->pad_last_buttons = input->pad_buttons;
}

View File

@ -1,46 +1,52 @@
#include <stdio.h>
#include "gfx_es2/draw_buffer.h"
#include "gfx/texture_atlas.h"
#include "input/input_state.h"
#include "virtual_input.h"
TouchButton::TouchButton(const Atlas *atlas, int imageIndex, int overlayImageIndex, int button, int rotationAngle)
: atlas_(atlas), imageIndex_(imageIndex), overlayImageIndex_(overlayImageIndex), button_(button), rotationAngle_(rotationAngle)
: atlas_(atlas), imageIndex_(imageIndex), overlayImageIndex_(overlayImageIndex), button_(button)
{
memset(pointerDown, 0, sizeof(pointerDown));
w_ = atlas->images[imageIndex].w;
h_ = atlas->images[imageIndex].h;
w_ = atlas_->images[imageIndex_].w;
h_ = atlas_->images[imageIndex_].h;
rotationAngle_ = (float)rotationAngle * 3.1415927 / 180.0f;
isDown_ = false;
}
void TouchButton::update(InputState &input_state)
{
bool isDown = false;
isDown_ = false;
for (int i = 0; i < MAX_POINTERS; i++) {
if (input_state.pointer_down[i] && isInside(input_state.pointer_x[i], input_state.pointer_y[i]))
isDown = true;
isDown_ = true;
}
if (isDown) {
int prev_buttons = input_state.pad_buttons;
if (isDown_) {
input_state.pad_buttons |= button_;
input_state.pad_buttons_down |= button_ & (~prev_buttons);
} else {
input_state.pad_buttons_up &= ~(button_ & input_state.pad_buttons);
input_state.pad_buttons &= ~button_;
}
}
void TouchButton::draw(DrawBuffer &db)
{
db.DrawImageRotated(imageIndex_, x_ + w_/2, y_ + h_/2, 1.0f, rotationAngle_, 0xFFFFFFFF);
uint32_t color = 0xAAFFFFFF;
float scale = 1.0f;
if (isDown_) {
color = 0xFFFFFFFF;
scale = 2.0f;
}
db.DrawImageRotated(imageIndex_, x_ + w_/2, y_ + h_/2, scale, rotationAngle_, color);
if (overlayImageIndex_ != -1)
db.DrawImageRotated(overlayImageIndex_, x_ + w_/2, y_ + h_/2, 1.0f, rotationAngle_, 0xFFFFFFFF);
db.DrawImageRotated(overlayImageIndex_, x_ + w_/2, y_ + h_/2, scale, rotationAngle_, color);
}
TouchStick::TouchStick(const Atlas *atlas, int bgImageIndex, int stickImageIndex, int stick)
: atlas_(atlas), bgImageIndex_(bgImageIndex), stickImageIndex_(stickImageIndex), stick_(stick)
{
stick_size_ = atlas_->images[bgImageIndex].w;
}
void TouchStick::update(InputState &input_state)
@ -77,4 +83,4 @@ void TouchStick::draw(DrawBuffer &db)
if (bgImageIndex_ != -1)
db.DrawImage(bgImageIndex_, stick_x_, stick_y_, 1.0f, 0xFFFFFFFF, ALIGN_CENTER);
db.DrawImage(stickImageIndex_, stick_x_ + stick_delta_x_, stick_y_ + stick_delta_y_, 1.0f, 0xFFFFFFFF, ALIGN_CENTER);
}
}

View File

@ -27,12 +27,14 @@ private:
int imageIndex_;
int overlayImageIndex_;
int button_;
int rotationAngle_;
float rotationAngle_;
float x_, y_;
float w_;
float h_;
bool isDown_;
// TODO: simplify into flags.
bool pointerDown[MAX_POINTERS];
};