This commit is contained in:
Henrik Rydgård 2012-10-28 11:37:10 +01:00
parent 4c62311132
commit 60d3b5f601
9 changed files with 114 additions and 23 deletions

View File

@ -272,7 +272,7 @@ extern "C" void Java_com_turboviking_libnative_NativeApp_keyDown
input_state.pad_buttons |= PAD_BUTTON_BACK;
break;
case 2: // Menu
input_state.pad_buttons |= PAD_BUTTON_START;
input_state.pad_buttons |= PAD_BUTTON_MENU;
break;
case 3: // Search
input_state.pad_buttons |= PAD_BUTTON_A;
@ -289,7 +289,7 @@ extern "C" void Java_com_turboviking_libnative_NativeApp_keyUp
input_state.pad_buttons &= ~PAD_BUTTON_BACK;
break;
case 2: // Menu
input_state.pad_buttons &= ~PAD_BUTTON_START;
input_state.pad_buttons &= ~PAD_BUTTON_MENU;
break;
case 3: // Search
input_state.pad_buttons &= ~PAD_BUTTON_A;

View File

@ -56,6 +56,8 @@ void LaunchBrowser(const char *url)
#ifdef _WIN32
ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
#else
ILOG("Would have gone to %s but LaunchBrowser is not implemented on this platform",
url);
#endif
}
@ -64,6 +66,8 @@ void LaunchMarket(const char *url)
#ifdef _WIN32
ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
#else
ILOG("Would have gone to %s but LaunchMarket is not implemented on this platform",
url);
#endif
}
@ -72,12 +76,14 @@ void LaunchEmail(const char *email_address)
#ifdef _WIN32
ShellExecute(NULL, "open", (std::string("mailto:") + email_address).c_str(), NULL, NULL, SW_SHOWNORMAL);
#else
ILOG("Would have opened your email client for %s but LaunchEmail is not implemented on this platform",
email_address);
#endif
}
const int buttonMappings[12] = {
const int buttonMappings[14] = {
SDLK_x, //A
SDLK_s, //B
SDLK_z, //X
@ -85,11 +91,13 @@ const int buttonMappings[12] = {
SDLK_w, //LBUMPER
SDLK_q, //RBUMPER
SDLK_1, //START
SDLK_2, //BACK
SDLK_2, //SELECT
SDLK_UP, //UP
SDLK_DOWN, //DOWN
SDLK_LEFT, //LEFT
SDLK_RIGHT, //RIGHT
SDLK_m, //MENU
SDLK_BACKSPACE, //BACK
};
void SimulateGamepad(const uint8 *keys, InputState *input) {
@ -98,7 +106,7 @@ void SimulateGamepad(const uint8 *keys, InputState *input) {
input->pad_lstick_y = 0;
input->pad_rstick_x = 0;
input->pad_rstick_y = 0;
for (int b = 0; b < 12; b++) {
for (int b = 0; b < 14; b++) {
if (keys[buttonMappings[b]])
input->pad_buttons |= (1<<b);
}

View File

@ -58,9 +58,54 @@ bool ReadFileToString(bool text_file, const char *filename, std::string &str)
}
#define DIR_SEP "/"
#define DIR_SEP_CHR '\\'
#ifndef METRO
// Remove any ending forward slashes from directory paths
// Modifies argument.
static void stripTailDirSlashes(std::string &fname)
{
if (fname.length() > 1)
{
size_t i = fname.length() - 1;
while (fname[i] == DIR_SEP_CHR)
fname[i--] = '\0';
}
return;
}
// Returns true if file filename exists
bool exists(const std::string &filename)
{
struct stat64 file_info;
std::string copy(filename);
stripTailDirSlashes(copy);
int result = stat64(copy.c_str(), &file_info);
return (result == 0);
}
// Returns true if filename is a directory
bool isDirectory(const std::string &filename)
{
struct stat64 file_info;
std::string copy(filename);
stripTailDirSlashes(copy);
int result = stat64(copy.c_str(), &file_info);
if (result < 0) {
WLOG("IsDirectory: stat failed on %s", filename.c_str());
return false;
}
return S_ISDIR(file_info.st_mode);
}
size_t getFilesInDir(const char *directory, std::vector<FileInfo> *files) {
size_t foundEntries = 0;
#ifdef _WIN32
@ -96,9 +141,14 @@ size_t getFilesInDir(const char *directory, std::vector<FileInfo> *files) {
((virtualName[0] == '.') && (virtualName[1] == '.') &&
(virtualName[2] == '\0')))
continue;
// Remove dotfiles (should be made optional?)
if (virtualName[0] == '.')
continue;
FileInfo info;
info.name = std::string(directory) + virtualName;
info.isDirectory = false; // TODO
info.name = virtualName;
info.fullName = std::string(directory) + "/" + virtualName;
info.isDirectory = isDirectory(info.fullName);
files->push_back(info);
#ifdef _WIN32
} while (FindNextFile(hFind, &ffd) != 0);
@ -127,6 +177,8 @@ void deleteFile(const char *file)
std::string getDir(const std::string &path)
{
if (path == "/")
return path;
int n = path.size() - 1;
while (n >= 0 && path[n] != '\\' && path[n] != '/')
n--;

View File

@ -12,10 +12,12 @@ bool readFileToString(bool text_file, const char *filename, std::string &str);
struct FileInfo
{
std::string name;
std::string fullName;
bool isDirectory;
};
size_t getFilesInDir(const char *directory, std::vector<FileInfo> *files);
void deleteFile(const char *file);
bool exists(const std::string &filename);
std::string getDir(const std::string &path);

View File

@ -12,11 +12,15 @@ enum {
PAD_BUTTON_LBUMPER = 16,
PAD_BUTTON_RBUMPER = 32,
PAD_BUTTON_START = 64,
PAD_BUTTON_BACK = 128,
PAD_BUTTON_SELECT = 128,
PAD_BUTTON_UP = 256,
PAD_BUTTON_DOWN = 512,
PAD_BUTTON_LEFT = 1024,
PAD_BUTTON_RIGHT = 2048,
// Android only
PAD_BUTTON_MENU = 4096,
PAD_BUTTON_BACK = 8192,
};
#ifndef MAX_POINTERS

View File

@ -2,7 +2,7 @@
#include "input/input_state.h"
#include "ui/screen.h"
Screen::Screen() { }
Screen::Screen() : screenManager_(0) { }
Screen::~Screen() { }
ScreenManager::ScreenManager() {
@ -30,6 +30,7 @@ void ScreenManager::switchScreen(Screen *screen) {
}
if (screen != currentScreen_) {
nextScreen_ = screen;
nextScreen_->setScreenManager(this);
}
}
@ -93,6 +94,7 @@ void ScreenManager::shutdown() {
}
void ScreenManager::push(Screen *screen) {
screen->setScreenManager(this);
dialog_.push_back(screen);
}

View File

@ -27,6 +27,8 @@ enum DialogResult {
DR_NO,
};
class ScreenManager;
class Screen {
public:
Screen();
@ -36,7 +38,11 @@ public:
virtual void deviceLost() {}
virtual void dialogFinished(const Screen *dialog, DialogResult result) {}
ScreenManager *screenManager() { return screenManager_; }
void setScreenManager(ScreenManager *sm) { screenManager_ = sm; }
private:
ScreenManager *screenManager_;
DISALLOW_COPY_AND_ASSIGN(Screen);
};

View File

@ -44,6 +44,10 @@ void UIUpdateMouse(int i, float x, float y, bool down) {
uistate.mousedown[i] = down;
}
void UIReset() {
memset(&uistate, 0, sizeof(uistate));
}
bool UIRegionHit(int i, int x, int y, int w, int h, int margin) {
// Input handling
if (uistate.mousex[i] < x - margin ||
@ -108,8 +112,9 @@ int UIButton(int id, const LayoutManager &layout, float w, const char *text, int
// Check whether the button should be hot, use a generous margin for touch ease
if (UIRegionHit(i, x, y, w, h, 8)) {
uistate.hotitem[i] = id;
if (uistate.activeitem[i] == 0 && uistate.mousedown[i])
if (uistate.activeitem[i] == 0 && uistate.mousedown[i]) {
uistate.activeitem[i] = id;
}
}
if (uistate.hotitem[i] == id) {
@ -126,9 +131,9 @@ int UIButton(int id, const LayoutManager &layout, float w, const char *text, int
// If button is hot and active, but mouse button is not
// down, the user must have clicked the button.
if (uistate.mousedown[i] == 0 &&
uistate.hotitem[i] == id &&
uistate.activeitem[i] == id) {
clicked = 1;
uistate.hotitem[i] == id &&
uistate.activeitem[i] == id) {
clicked = 1;
}
}
@ -248,9 +253,12 @@ void StringVectorListAdapter::drawItem(int item, int x, int y, int w, int h, boo
ui_draw2d.DrawTextShadow(theme.uiFont, (*items_)[item].c_str(), x + UI_SPACE , y, 0xFFFFFFFF, ALIGN_LEFT | ALIGN_VCENTER);
}
int UIList::Do(int id, int x, int y, int w, int h, UIListAdapter *adapter) {
const int item_h = 64;
UIList::UIList()
: scrollY(0.0f), startDragY(0.0f), dragFinger(-1), selected(-1) {
}
int UIList::Do(int id, int x, int y, int w, int h, UIListAdapter *adapter) {
int clicked = 0;
for (int i = 0; i < MAX_POINTERS; i++) {
@ -276,13 +284,18 @@ int UIList::Do(int id, int x, int y, int w, int h, UIListAdapter *adapter) {
int numItems = adapter->getCount();
for (int i = 0; i < numItems; i++) {
int item_y = y + i * itemHeight - scrollY;
if (uistate.mousedown &&
adapter->itemEnabled(i) &&
item_y >= y - itemHeight &&
item_y <= y + h &&
UIRegionHit(i, x, item_y, w, h, 0)) {
selected = i;
}
for (int k = 0; k < MAX_POINTERS; k++) {
if (uistate.mousedown[k] &&
uistate.mouseframesdown[k] > 10 &&
adapter->itemEnabled(i) &&
item_y >= y - itemHeight &&
item_y <= y + h &&
UIRegionHit(k, x, item_y, w, itemHeight, 0)) {
printf("%i", item_y);
selected = i;
}
}
adapter->drawItem(i, x, item_y, w, itemHeight, i == selected);
}
uistate.lastwidget = id;

View File

@ -86,6 +86,7 @@ private:
// Mouse out of habit, applies just as well to touch events.
// UI does not yet support multitouch.
// This struct is zeroed on init, so should be valid at that state.
// Never inherit from this.
struct UIState {
int mousex[MAX_POINTERS];
int mousey[MAX_POINTERS];
@ -179,6 +180,9 @@ void UIBegin();
void UIUpdateMouse(int i, float x, float y, bool down);
// Call when you switch screens
void UIReset();
// Returns 1 if clicked
int UIButton(int id, const LayoutManager &layout, float w, const char *text, int button_align);
int UIImageButton(int id, const LayoutManager &layout, float w, int image_id, int button_align); // uses current UI atlas for fetching images.
@ -202,7 +206,7 @@ void UISlideChoice(int id, int y, const SlideItem *items, int numItems, UISlideS
class UIList {
public:
UIList() : scrollY(0.0f), startDragY(0.0f), dragFinger(-1), selected(-1) {}
UIList();
float scrollY;
float startDragY;
int dragFinger;