1
0

Implement button caption

This commit is contained in:
otya128
2016-12-26 15:47:52 +09:00
parent 106d64a8ef
commit 3182c2605f
3 changed files with 70 additions and 13 deletions

View File

@@ -3,6 +3,7 @@ import otya.smilebasic.petitcomputer;
import otya.smilebasic.project; import otya.smilebasic.project;
import derelict.sdl2.sdl; import derelict.sdl2.sdl;
import derelict.sdl2.image; import derelict.sdl2.image;
import derelict.sdl2.ttf;
import derelict.opengl3.gl; import derelict.opengl3.gl;
import derelict.opengl3.gl3; import derelict.opengl3.gl3;
@@ -39,17 +40,24 @@ enum ButtonResult
class DialogButton class DialogButton
{ {
int x, y, width, height; int x, y, width, height;
SDL_Rect foregroundRect;
GraphicPage background; GraphicPage background;
GraphicPage foreground;
DialogResult result; DialogResult result;
bool isPressed; bool isPressed;
this(GraphicPage background, int x, int y, DialogResult result) this(GraphicPage background, GraphicPage foreground, int x, int y, DialogResult result, int fx, int fy)
{ {
this.background = background; this.background = background;
this.foreground = foreground;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.width = background.surface.w; this.width = background.surface.w;
this.height = background.surface.h; this.height = background.surface.h;
this.result = result; this.result = result;
foregroundRect.x = fx;
foregroundRect.y = fy;
foregroundRect.w = foreground.surface.w;
foregroundRect.h = foreground.surface.h;
} }
bool colDetect(int x, int y) bool colDetect(int x, int y)
{ {
@@ -61,15 +69,33 @@ class DialogResources
{ {
public GraphicPage button1; public GraphicPage button1;
public GraphicPage button2; public GraphicPage button2;
public TTF_Font* font;
public this(SDL_Renderer* renderer, GLenum textureScaleMode) public this(SDL_Renderer* renderer, GLenum textureScaleMode)
{ {
button1 = new GraphicPage("dialogresource/yes.png"); button1 = new GraphicPage("dialogresource/yes.png");
button1.createTexture(renderer, textureScaleMode); button1.createTexture(renderer, textureScaleMode);
button2 = new GraphicPage("dialogresource/no.png"); button2 = new GraphicPage("dialogresource/no.png");
button2.createTexture(renderer, textureScaleMode); button2.createTexture(renderer, textureScaleMode);
font = TTF_OpenFont("dialogresource/mplus-1c-regular.ttf", 14);
} }
} }
struct Text
{
GraphicPage texture;
this(PetitComputer petitcom, wstring text, SDL_Color color)
{
text ~= '\0';
auto surface = petitcom.dialogResource.font.TTF_RenderUNICODE_Blended((cast(ushort[])text).ptr, color);
texture = new GraphicPage(surface);
texture.createTexture(petitcom.renderer, petitcom.textureScaleMode);
}
~this()
{
texture.deleteGL();
texture.deleteSDL();
}
}
class Dialog : DialogBase class Dialog : DialogBase
{ {
@@ -109,6 +135,19 @@ class Dialog : DialogBase
glEnd(); glEnd();
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE);
} }
void renderQuad(SDL_Rect rect)
{
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex2i(rect.x, rect.y);
glTexCoord2i(1, 0);
glVertex2i(rect.x + rect.w, rect.y);
glTexCoord2i(1, 1);
glVertex2i(rect.x + rect.w, rect.y + rect.h);
glTexCoord2i(0, 1);
glVertex2i(rect.x, rect.y + rect.h);
glEnd();
}
void renderButton(DialogButton button) void renderButton(DialogButton button)
{ {
int mx, my; int mx, my;
@@ -119,18 +158,16 @@ class Dialog : DialogBase
} }
glDepthMask(GL_FALSE); glDepthMask(GL_FALSE);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glColor3ub(255, 255, 255); glColor3ub(255, 255, 255);
glBindTexture(GL_TEXTURE_2D, button.background.glTexture); glBindTexture(GL_TEXTURE_2D, button.background.glTexture);
glBegin(GL_QUADS); renderQuad(SDL_Rect(button.x + mx, button.y + my, button.width, button.height));
glTexCoord2i(0, 0); if (button.foreground)
glVertex2i(button.x + mx, button.y + my); {
glTexCoord2i(1, 0); glBindTexture(GL_TEXTURE_2D, button.foreground.glTexture);
glVertex2i(button.x + button.width + mx, button.y + my); renderQuad(SDL_Rect(button.x + button.foregroundRect.x + mx, button.y + button.foregroundRect.y + my, button.foregroundRect.w, button.foregroundRect.h));
glTexCoord2i(1, 1); }
glVertex2i(button.x + button.width + mx, button.y + button.height + my); glDisable(GL_BLEND);
glTexCoord2i(0, 1);
glVertex2i(button.x + mx, button.y + button.height + my);
glEnd();
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE);
} }
@@ -145,24 +182,36 @@ class Dialog : DialogBase
} }
int show(wstring text) int show(wstring text)
{ {
petitcom.dialog = this; petitcom.dialog = this;
area = petitcom.showTouchScreen(); area = petitcom.showTouchScreen();
int buttonMarginWidth = 12; int buttonMarginWidth = 12;
int buttonMarginHeight = 12; int buttonMarginHeight = 12;
auto button1text = Text(petitcom, "はい", SDL_Color(0, 0, 0, 255));
ubyte sr, sg, sb, sa;
SDL_GetRGBA((cast(uint*)petitcom.dialogResource.button2.surface.pixels)[0], petitcom.dialogResource.button2.surface.format, &sr, &sg, &sb, &sa);
auto button2text = Text(petitcom, "いいえ", SDL_Color(sr, sg, sb, 255));
buttons = [ buttons = [
new DialogButton( new DialogButton(
petitcom.dialogResource.button1, petitcom.dialogResource.button1,
button1text.texture,
area.w - buttonMarginWidth - petitcom.dialogResource.button1.surface.w, area.w - buttonMarginWidth - petitcom.dialogResource.button1.surface.w,
area.h - buttonMarginHeight - petitcom.dialogResource.button1.surface.h, area.h - buttonMarginHeight - petitcom.dialogResource.button1.surface.h,
DialogResult.SUCCESS DialogResult.SUCCESS,
22,
1
), ),
new DialogButton( new DialogButton(
petitcom.dialogResource.button2, petitcom.dialogResource.button2,
button2text.texture,
buttonMarginWidth, buttonMarginWidth,
area.h - buttonMarginHeight - petitcom.dialogResource.button1.surface.h, area.h - buttonMarginHeight - petitcom.dialogResource.button1.surface.h,
DialogResult.CANCEL DialogResult.CANCEL,
22,
1
) )
]; ];
bool isPressed; bool isPressed;

Binary file not shown.

View File

@@ -151,6 +151,11 @@ class GraphicPage
glDeleteTextures(1, &glTexture); glDeleteTextures(1, &glTexture);
buffer = render = glTexture = 0; buffer = render = glTexture = 0;
} }
void deleteSDL()
{
SDL_FreeSurface(surface);
surface = null;
}
} }
@@ -684,8 +689,11 @@ class PetitComputer
{ {
try try
{ {
import derelict.sdl2.ttf;
DerelictSDL2.load(); DerelictSDL2.load();
DerelictSDL2Image.load(); DerelictSDL2Image.load();
DerelictSDL2ttf.load();
TTF_Init();
sprite.sppage[] = 4; sprite.sppage[] = 4;
bgpage[] = 5; bgpage[] = 5;
SDL_Init(SDL_INIT_VIDEO); SDL_Init(SDL_INIT_VIDEO);