Image buttons, file dialogs (win only)

This commit is contained in:
Henrik Rydgard 2012-04-12 15:18:43 +02:00
parent 2993ff1a7b
commit 371c4db1ae
7 changed files with 136 additions and 2 deletions

View File

@ -2,7 +2,8 @@ set(SRCS
easy_file.cpp
chunk_file.cpp
zip_read.cpp
file_util.cpp)
file_util.cpp
dialog.cpp)
set(SRCS ${SRCS})

8
file/dialog.cpp Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include <string>
#include "file/dialog.h"
bool OpenFileDialog(const char *title, const char *extension, std::string *filename);
bool SaveFileDialog(const char *title, const char *extension, std::string *filename);

73
file/dialog.h Normal file
View File

@ -0,0 +1,73 @@
#pragma once
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>
#include <string>
// For desktop operating systems only. Stubbed out on Android.
// Simplified as this will only be used in utilities / temp code.
// An false returned means cancel;
bool OpenFileDialog(const char *title, const char *extension, std::string *filename)
{
OPENFILENAME of;
memset(&of, 0, sizeof(of));
char buffer[512] = {0};
of.lStructSize = sizeof(OPENFILENAME);
of.hInstance = 0;
of.hwndOwner = GetActiveWindow();
of.lpstrFilter = "All files (*.*)\0*.*\0\0";
of.lpstrDefExt = extension;
of.lpstrFile = buffer;
of.nMaxFile = 511;
of.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (!GetOpenFileName(&of)) return false;
*filename = of.lpstrFile;
return true;
}
bool SaveFileDialog(const char *title, const char *extension, std::string *filename)
{
OPENFILENAME of;
memset(&of, 0, sizeof(of));
char buffer[512] = {0};
of.lStructSize = sizeof(OPENFILENAME);
of.hInstance = 0;
of.hwndOwner = GetActiveWindow();
of.lpstrFilter = "All files (*.*)\0*.*\0\0";
of.lpstrDefExt = extension;
of.lpstrFile = buffer;
of.nMaxFile = 511;
of.Flags = OFN_HIDEREADONLY;
if (!GetSaveFileName(&of))
return false;
*filename = of.lpstrFile;
return true;
}
#else
#include <string>
#include "base/basictypes.h"
bool OpenFileDialog(const char *title, const char *extension, std::string *filename)
{
ELOG("Asked for OpenFileDialog, not present on this platform.");
return false;
}
bool SaveFileDialog(const char *title, const char *extension, std::string *filename)
{
ELOG("Asked for SaveFileDialog, not present on this platform.");
return false;
}
#endif

View File

@ -12,7 +12,7 @@
#include "file/file_util.h"
#include <sys/stat.h>
bool WriteStringToFile(bool text_file, const std::string &str, const char *filename)
bool writeStringToFile(bool text_file, const std::string &str, const char *filename)
{
FILE *f = fopen(filename, text_file ? "w" : "wb");
if (!f)

View File

@ -107,6 +107,7 @@
<ClInclude Include="ext\vjson\block_allocator.h" />
<ClInclude Include="ext\vjson\json.h" />
<ClInclude Include="file\chunk_file.h" />
<ClInclude Include="file\dialog.h" />
<ClInclude Include="file\easy_file.h" />
<ClInclude Include="file\file_util.h" />
<ClInclude Include="file\vfs.h" />
@ -155,6 +156,7 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="file\chunk_file.cpp" />
<ClCompile Include="file\dialog.cpp" />
<ClCompile Include="file\easy_file.cpp" />
<ClCompile Include="file\file_util.cpp" />
<ClCompile Include="file\zip_read.cpp" />

View File

@ -150,6 +150,9 @@
<ClInclude Include="midi\midi_input.h">
<Filter>midi</Filter>
</ClInclude>
<ClInclude Include="file\dialog.h">
<Filter>file</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="gfx\gl_debug_log.cpp">
@ -269,6 +272,9 @@
<ClCompile Include="midi\midi_input.cpp">
<Filter>midi</Filter>
</ClCompile>
<ClCompile Include="file\dialog.cpp">
<Filter>file</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="gfx">

View File

@ -122,6 +122,50 @@ int UIButton(int id, int x, int y, int w, const char *text, int button_align) {
return clicked;
}
int UIImageButton(int id, int x, int y, int w, int image, int button_align) {
const int h = 64;
if (button_align & ALIGN_HCENTER) x -= w / 2;
if (button_align & ALIGN_VCENTER) y -= h / 2;
if (button_align & ALIGN_RIGHT) x -= w;
if (button_align & ALIGN_BOTTOMRIGHT) y -= h;
// Check whether the button should be hot, use a generous margin for touch ease
if (UIRegionHit(x, y, w, h, 8)) {
uistate.hotitem = id;
if (uistate.activeitem == 0 && uistate.mousedown)
uistate.activeitem = id;
}
// Render button
int txOffset = 0;
if (uistate.hotitem == id) {
if (uistate.activeitem == id) {
// Button is both 'hot' and 'active'
txOffset = 2;
} else {
// Button is merely 'hot'
}
} else {
// button is not hot, but it may be active
}
ui_draw2d.DrawImage2GridH(themeButtonImage, x, y, x + w);
ui_draw2d.DrawImage(image, x + w/2, y + h/2 + txOffset, 0xFFFFFFFF, ALIGN_HCENTER | ALIGN_VCENTER);
int clicked = 0;
// If button is hot and active, but mouse button is not
// down, the user must have clicked the button.
if (uistate.mousedown == 0 &&
uistate.hotitem == id &&
uistate.activeitem == id) {
clicked = 1;
}
uistate.lastwidget = id;
return clicked;
}
int UICheckBox(int id, int x, int y, const char *text, int align, bool *value) {
const int h = 64;
float tw, th;