initial commit
This commit is contained in:
205
NewDialogs/Button.cpp
Normal file
205
NewDialogs/Button.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
#include "../SDLHelpers.h"
|
||||
|
||||
#include "Button.h"
|
||||
|
||||
Button::Button(string text, SDL_Color color, SDL_Rect pos) : Item(pos) {
|
||||
this->color = color;
|
||||
this->text = text;
|
||||
pressed = false;
|
||||
selected = false;
|
||||
toggle_selected = false;
|
||||
button_func = NULL;
|
||||
button_press_func = NULL;
|
||||
button_release_func = NULL;
|
||||
drawButton();
|
||||
selectSurface();
|
||||
}
|
||||
|
||||
Button::~Button() {
|
||||
SDL_DestroySurface(button_up);
|
||||
SDL_DestroySurface(button_down);
|
||||
}
|
||||
|
||||
/*void Button::init() {
|
||||
buttonPressed = false;
|
||||
drawButton();
|
||||
setSurface(buttonUp);
|
||||
}*/
|
||||
|
||||
/*void Button::destroy() {
|
||||
SDL_FreeSurface(buttonUp);
|
||||
SDL_FreeSurface(buttonDown);
|
||||
}*/
|
||||
|
||||
void Button::drawButton() {
|
||||
//Uint32 color = 0xFFE6DCDC;
|
||||
//SDL_Color color = 0xFFB4C8B4;
|
||||
//Uint32 color = 0xFFB4B4C8;
|
||||
SDL_Color color_reflection = color;
|
||||
SDL_Color color_shadow = color;
|
||||
color_reflection.r = (int) color_reflection.r + 50 < 255 ? color_reflection.r + 50 : 255;
|
||||
color_reflection.g = (int) color_reflection.g + 50 < 255 ? color_reflection.g + 50 : 255;
|
||||
color_reflection.b = (int) color_reflection.b + 50 < 255 ? color_reflection.b + 50 : 255;
|
||||
color_shadow.r = (int) color_shadow.r - 50 > 0 ? color_shadow.r - 50 : 0;
|
||||
color_shadow.g = (int) color_shadow.g - 50 > 0 ? color_shadow.g - 50 : 0;
|
||||
color_shadow.b = (int) color_shadow.b - 50 > 0 ? color_shadow.b - 50 : 0;
|
||||
|
||||
int w = getPos()->w;
|
||||
int h = getPos()->h;
|
||||
button_up = SDL_CreateSurface(w, h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000));
|
||||
button_down = SDL_CreateSurface(w, h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000));
|
||||
Uint32 *p_up = (Uint32 *) button_up->pixels;
|
||||
Uint32 *p_down = (Uint32 *) button_down->pixels;
|
||||
SDL_Color color_up;
|
||||
SDL_Color color_down;
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
if (y == 0 || y == h-1 || x == 0 || x == w-1) {
|
||||
color_up.r = 0;
|
||||
color_up.g = 0;
|
||||
color_up.b = 0;
|
||||
color_down = color_up;
|
||||
} else if (y == 1 || x == 1) {
|
||||
color_up = color_reflection;
|
||||
color_down = color_shadow;
|
||||
} else if (y == h-2 || x == w-2) {
|
||||
color_up = color_shadow;
|
||||
color_down = color_reflection;
|
||||
} else {
|
||||
color_up = color;
|
||||
if ((y + x) % 2 == 0) {
|
||||
color_down = color;
|
||||
} else {
|
||||
color_down.r = 255;
|
||||
color_down.g = 255;
|
||||
color_down.b = 255;
|
||||
}
|
||||
}
|
||||
*p_up = SDL_MapRGB(SDL_GetPixelFormatDetails(button_up->format), NULL, color_up.r, color_up.g, color_up.b);
|
||||
*p_down = SDL_MapRGB(SDL_GetPixelFormatDetails(button_down->format), NULL, color_down.r, color_down.g, color_down.b);
|
||||
p_up++;
|
||||
p_down++;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Color text_color = {0, 0, 0};
|
||||
vector<string> fonts;
|
||||
fonts.push_back("DejaVuSans.ttf");
|
||||
fonts.push_back("ariblk.ttf");
|
||||
fonts.push_back("FreeSans.ttf");
|
||||
fonts.push_back("arial.ttf");
|
||||
TTF_Font *font = openFont(fonts, 10);
|
||||
SDL_Surface *text_surface;
|
||||
if (!font)
|
||||
cout << "TTF_OpenFont is broken!" << endl;
|
||||
if(!(text_surface = TTF_RenderText_Blended(font, text.c_str(), text.length(), text_color)))
|
||||
cout << SDL_GetError() << endl;
|
||||
SDL_Rect pos;
|
||||
pos.x = button_up->w / 2 - text_surface->w / 2;
|
||||
pos.y = button_up->h / 2 - text_surface->h / 2;
|
||||
SDL_BlitSurface(text_surface, NULL, button_up, &pos);
|
||||
SDL_BlitSurface(text_surface, NULL, button_down, &pos);
|
||||
SDL_DestroySurface(text_surface);
|
||||
TTF_CloseFont(font);
|
||||
}
|
||||
|
||||
bool Button::onMousePress(int x, int y) {
|
||||
bool isInX = x > getPos()->x && x < getPos()->x + getPos()->w;
|
||||
bool isInY = y > getPos()->y && y < getPos()->y + getPos()->h;
|
||||
if (isInX && isInY) {
|
||||
pressed = true;
|
||||
if (button_press_func) {
|
||||
(*button_press_func)(p, this);
|
||||
}
|
||||
selectSurface();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Button::onMouseRelease(int x, int y) {
|
||||
if (pressed) {
|
||||
bool is_in_x = x > getPos()->x && x < getPos()->x + getPos()->w;
|
||||
bool is_in_y = y > getPos()->y && y < getPos()->y + getPos()->h;
|
||||
if (is_in_x && is_in_y) {
|
||||
if (toggle_selected) {
|
||||
selected = !selected;
|
||||
}
|
||||
if (button_func) {
|
||||
(*button_func)(p, this);
|
||||
}
|
||||
}
|
||||
if (button_release_func) {
|
||||
(*button_release_func)(p, this);
|
||||
}
|
||||
pressed = false;
|
||||
selectSurface();
|
||||
}
|
||||
}
|
||||
|
||||
void Button::onFingerDown(int x, int y, int finger_id) {
|
||||
if (onMousePress(x, y)) {
|
||||
this->finger_id = finger_id;
|
||||
}
|
||||
}
|
||||
|
||||
void Button::onFingerUp(int x, int y, int finger_id) {
|
||||
if (pressed && this->finger_id == finger_id) {
|
||||
onMouseRelease(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void Button::setButtonFunc(void (*button_func)(void *p, Item *b), void *p) {
|
||||
this->button_func = button_func;
|
||||
this->p = p;
|
||||
}
|
||||
|
||||
void Button::setButtonPressFunc(void (*button_press_func)(void *p, Item *b), void *p) {
|
||||
this->button_press_func = button_press_func;
|
||||
this->p = p;
|
||||
}
|
||||
|
||||
void Button::setButtonReleaseFunc(void (*button_release_func)(void *p, Item *b), void *p) {
|
||||
this->button_release_func = button_release_func;
|
||||
this->p = p;
|
||||
}
|
||||
|
||||
void Button::setText(string text) {
|
||||
this->text = text;
|
||||
}
|
||||
|
||||
string Button::getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
void Button::setColor(SDL_Color color) {
|
||||
this->color = color;
|
||||
}
|
||||
|
||||
void Button::setSelected(bool selected) {
|
||||
this->selected = selected;
|
||||
selectSurface();
|
||||
}
|
||||
|
||||
bool Button::isSelected() {
|
||||
return selected;
|
||||
}
|
||||
|
||||
void Button::selectSurface() {
|
||||
if (pressed) {
|
||||
setSurface(button_down);
|
||||
} else {
|
||||
if (selected) {
|
||||
setSurface(button_down);
|
||||
} else {
|
||||
setSurface(button_up);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
NewDialogs/Button.h
Normal file
44
NewDialogs/Button.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef BUTTON_H
|
||||
#define BUTTON_H
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "Item.h"
|
||||
|
||||
class Button : public Item {
|
||||
protected:
|
||||
string text;
|
||||
SDL_Color color;
|
||||
SDL_Surface *button_up;
|
||||
SDL_Surface *button_down;
|
||||
bool toggle_selected;
|
||||
private:
|
||||
bool pressed;
|
||||
bool selected;
|
||||
int finger_id;
|
||||
void (*button_func)(void *p, Item *b);
|
||||
void (*button_press_func)(void *p, Item *b);
|
||||
void (*button_release_func)(void *p, Item *b);
|
||||
void *p;
|
||||
public:
|
||||
//void init();
|
||||
//void destroy();
|
||||
Button(string text, SDL_Color color, SDL_Rect pos);
|
||||
virtual ~Button();
|
||||
virtual void drawButton();
|
||||
bool onMousePress(int x, int y);
|
||||
void onMouseRelease(int x, int y);
|
||||
void onFingerDown(int x, int y, int finger_id);
|
||||
void onFingerUp(int x, int y, int finger_id);
|
||||
void setButtonFunc(void (*button_func)(void *p, Item *b), void *p);
|
||||
void setButtonPressFunc(void (*button_press_func)(void *p, Item *b), void *p);
|
||||
void setButtonReleaseFunc(void (*button_release_func)(void *p, Item *b), void *p);
|
||||
void setText(string text);
|
||||
string getText();
|
||||
void setColor(SDL_Color color);
|
||||
void setSelected(bool selected);
|
||||
bool isSelected();
|
||||
void selectSurface();
|
||||
};
|
||||
|
||||
#endif
|
||||
117
NewDialogs/Checkbox.cpp
Normal file
117
NewDialogs/Checkbox.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
#include "../SDLHelpers.h"
|
||||
|
||||
#include "Checkbox.h"
|
||||
|
||||
bool isBetween(float a, float b, float c) {
|
||||
return a >= b && a < c;
|
||||
}
|
||||
|
||||
Checkbox::Checkbox(string text, SDL_Color color, SDL_Rect pos) : Button(text, color, pos) {
|
||||
//SDL_Color color;
|
||||
//Button::Button(text, color, pos);
|
||||
toggle_selected = true;
|
||||
// Parent Button doesn't seem to call this but it's own drawButton. So doing it again for here. FIXME how to fix?
|
||||
SDL_DestroySurface(button_up);
|
||||
SDL_DestroySurface(button_down);
|
||||
drawButton();
|
||||
selectSurface();
|
||||
}
|
||||
|
||||
Checkbox::~Checkbox() {
|
||||
}
|
||||
|
||||
void Checkbox::drawButton() {
|
||||
SDL_Color color_reflection = color;
|
||||
SDL_Color color_shadow = color;
|
||||
color_reflection.r = (int) color_reflection.r + 50 < 255 ? color_reflection.r + 50 : 255;
|
||||
color_reflection.g = (int) color_reflection.g + 50 < 255 ? color_reflection.g + 50 : 255;
|
||||
color_reflection.b = (int) color_reflection.b + 50 < 255 ? color_reflection.b + 50 : 255;
|
||||
color_shadow.r = (int) color_shadow.r - 50 > 0 ? color_shadow.r - 50 : 0;
|
||||
color_shadow.g = (int) color_shadow.g - 50 > 0 ? color_shadow.g - 50 : 0;
|
||||
color_shadow.b = (int) color_shadow.b - 50 > 0 ? color_shadow.b - 50 : 0;
|
||||
|
||||
int w = getPos()->w;
|
||||
int h = getPos()->h;
|
||||
button_up = SDL_CreateSurface(w, h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000));
|
||||
button_down = SDL_CreateSurface(w, h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000));
|
||||
Uint32 *p_up = (Uint32 *) button_up->pixels;
|
||||
Uint32 *p_down = (Uint32 *) button_down->pixels;
|
||||
SDL_Color color_up;
|
||||
SDL_Color color_down;
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) { // checkbox needs to be square so using height
|
||||
float u = h / 12.0; // one unit (was 1 pixel in example image)
|
||||
color_up.a = 0xff;
|
||||
color_down.a = 0xff;
|
||||
if (x > h) {
|
||||
color_up.a = 0;
|
||||
color_down.a = 0;
|
||||
} else if (isBetween(y, h - u, h) || isBetween(x, h - u, h)) { // reflection
|
||||
color_up.r = 0xd4;
|
||||
color_up.g = 0xd0;
|
||||
color_up.b = 0xc8;
|
||||
color_down = color_up;
|
||||
} else if (isBetween(y, 0, u) || isBetween(x, 0, u)) { // lighter shadow
|
||||
color_up.r = 0x80;
|
||||
color_up.g = 0x80;
|
||||
color_up.b = 0x80;
|
||||
color_down = color_up;
|
||||
} else if (isBetween(y, u, u * 2) || isBetween(x, u, u * 2)) { // darker shadow
|
||||
color_up.r = 0x40;
|
||||
color_up.g = 0x40;
|
||||
color_up.b = 0x40;
|
||||
color_down = color_up;
|
||||
} else {
|
||||
color_up.r = 255;
|
||||
color_up.g = 255;
|
||||
color_up.b = 255;
|
||||
color_down = color_up;
|
||||
bool x_cond = isBetween(x, u * 3, 10 * u);
|
||||
bool y_cond = isBetween(y, x + u * 1.5, x + u * 4.5);
|
||||
float x_change_limit = 5.5 * u;
|
||||
if (x >= x_change_limit) {
|
||||
unsigned remaining_x = (x - x_change_limit);
|
||||
y_cond = isBetween(y, u * 7 - remaining_x, u * 10 - remaining_x);
|
||||
}
|
||||
if (x_cond && y_cond) {
|
||||
color_down.r = 0;
|
||||
color_down.g = 0;
|
||||
color_down.b = 0;
|
||||
}
|
||||
}
|
||||
*p_up = SDL_MapRGBA(SDL_GetPixelFormatDetails(button_up->format), NULL, color_up.r, color_up.g, color_up.b, color_up.a);
|
||||
*p_down = SDL_MapRGBA(SDL_GetPixelFormatDetails(button_down->format), NULL, color_down.r, color_down.g, color_down.b, color_down.a);
|
||||
p_up++;
|
||||
p_down++;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Color text_color = {0, 0, 0};
|
||||
vector<string> fonts;
|
||||
fonts.push_back("DejaVuSans.ttf");
|
||||
fonts.push_back("ariblk.ttf");
|
||||
fonts.push_back("FreeSans.ttf");
|
||||
fonts.push_back("arial.ttf");
|
||||
TTF_Font *font = openFont(fonts, 10);
|
||||
SDL_Surface *text_surface;
|
||||
if (!font) {
|
||||
SDL_Log("TTF_OpenFont is broken!");
|
||||
}
|
||||
if(!(text_surface = TTF_RenderText_Blended(font, text.c_str(), text.length(), text_color))) {
|
||||
SDL_Log("RenderText failed %s", SDL_GetError());
|
||||
}
|
||||
SDL_Rect pos;
|
||||
pos.x = button_up->h + 8;
|
||||
pos.y = button_up->h / 2 - text_surface->h / 2;
|
||||
SDL_BlitSurface(text_surface, NULL, button_up, &pos);
|
||||
SDL_BlitSurface(text_surface, NULL, button_down, &pos);
|
||||
SDL_DestroySurface(text_surface);
|
||||
TTF_CloseFont(font);
|
||||
}
|
||||
14
NewDialogs/Checkbox.h
Normal file
14
NewDialogs/Checkbox.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef CHECKBOX_H
|
||||
#define CHECKBOX_H
|
||||
|
||||
#include "Button.h"
|
||||
|
||||
class Checkbox : public Button {
|
||||
private:
|
||||
public:
|
||||
Checkbox(string text, SDL_Color color, SDL_Rect pos);
|
||||
virtual ~Checkbox();
|
||||
virtual void drawButton() override;
|
||||
};
|
||||
|
||||
#endif
|
||||
128
NewDialogs/Dialog.cpp
Normal file
128
NewDialogs/Dialog.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
|
||||
#include "Item.h"
|
||||
#include "Dialog.h"
|
||||
|
||||
bool Dialog::loadBackground(string name) {
|
||||
background = IMG_Load(name.c_str());
|
||||
if (!background)
|
||||
return false;
|
||||
surface = SDL_CreateSurface(background->w, background->h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000));
|
||||
SDL_Rect d;
|
||||
d.x = 0;
|
||||
d.y = 0;
|
||||
d.w = background->w;
|
||||
d.h = background->h;
|
||||
SDL_SetSurfaceClipRect(surface, &d);
|
||||
pos.w = background->w;
|
||||
pos.h = background->h;
|
||||
default_pos = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Dialog::addItem(Item *item) {
|
||||
items.push_back(item);
|
||||
}
|
||||
|
||||
void Dialog::deleteItems() {
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
delete items[z];
|
||||
}
|
||||
}
|
||||
|
||||
bool Dialog::needsRedraw() {
|
||||
return true; // always drawing, only performance optimizaion on some dialogs which override this method
|
||||
}
|
||||
|
||||
void Dialog::draw() {
|
||||
if (background) {
|
||||
SDL_BlitSurface(background, NULL, surface, NULL);
|
||||
}
|
||||
for (unsigned int x = 0; x < items.size(); x++) {
|
||||
SDL_BlitSurface(items[x]->getSurface(), NULL, surface, items[x]->getPos());
|
||||
items[x]->drawDone();
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Surface *Dialog::getSurface() {
|
||||
if (needsRedraw()) {
|
||||
draw();
|
||||
}
|
||||
return surface;
|
||||
}
|
||||
|
||||
void Dialog::deleteSurfaceAndBackground() {
|
||||
delete background;
|
||||
delete surface;
|
||||
}
|
||||
|
||||
bool Dialog::onMousePress(int x, int y) {
|
||||
if (x < 0 || y < 0 || x > surface->w || y > surface->h) {
|
||||
exit = true;
|
||||
return false;
|
||||
}
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
items[z]->onMousePress(x, y);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Dialog::onMouseRelease(int x, int y) {
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
items[z]->onMouseRelease(x, y);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Dialog::onMouseMove(int x, int y) {
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
items[z]->onMouseMove(x, y);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Dialog::onFingerDown(int x, int y, int finger_id) {
|
||||
if (x < 0 || y < 0 || x > surface->w || y > surface->h) {
|
||||
exit = true;
|
||||
return false;
|
||||
}
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
items[z]->onFingerDown(x, y, finger_id);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Dialog::onFingerUp(int x, int y, int finger_id) {
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
items[z]->onFingerUp(x, y, finger_id);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Dialog::setPos(SDL_Rect pos) {
|
||||
this->pos = pos;
|
||||
}
|
||||
|
||||
SDL_Rect *Dialog::getPos() {
|
||||
return &pos;
|
||||
}
|
||||
|
||||
void Dialog::setExit(bool exit) {
|
||||
this->exit = exit;
|
||||
}
|
||||
|
||||
bool Dialog::isExit() {
|
||||
return exit;
|
||||
}
|
||||
|
||||
bool Dialog::isDefaultPos() {
|
||||
return default_pos;
|
||||
}
|
||||
|
||||
void Dialog::setDefaultPos(bool default_pos) {
|
||||
this->default_pos = default_pos;
|
||||
}
|
||||
42
NewDialogs/Dialog.h
Normal file
42
NewDialogs/Dialog.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef DIALOG_H
|
||||
#define DIALOG_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "Item.h"
|
||||
|
||||
class Dialog {
|
||||
protected:
|
||||
SDL_Surface *surface;
|
||||
SDL_Surface *background;
|
||||
SDL_Rect pos;
|
||||
vector<Item *> items;
|
||||
private:
|
||||
bool default_pos;
|
||||
bool exit;
|
||||
public:
|
||||
bool loadBackground(string name);
|
||||
void addItem(Item *item);
|
||||
void deleteItems();
|
||||
virtual bool needsRedraw();
|
||||
virtual void draw();
|
||||
SDL_Surface *getSurface();
|
||||
void deleteSurfaceAndBackground();
|
||||
virtual bool onMousePress(int x, int y);
|
||||
virtual bool onMouseRelease(int x, int y);
|
||||
virtual bool onMouseMove(int x, int y);
|
||||
virtual bool onFingerDown(int x, int y, int finger_id);
|
||||
virtual bool onFingerUp(int x, int y, int finger_id);
|
||||
void setPos(SDL_Rect pos);
|
||||
SDL_Rect *getPos();
|
||||
void setExit(bool exit);
|
||||
bool isExit();
|
||||
bool isDefaultPos();
|
||||
void setDefaultPos(bool default_pos);
|
||||
};
|
||||
|
||||
#endif
|
||||
69
NewDialogs/DlgDropDownList.cpp
Normal file
69
NewDialogs/DlgDropDownList.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
#include "../SDLHelpers.h"
|
||||
|
||||
#include "Item.h"
|
||||
#include "Scrollbar.h"
|
||||
#include "Listbox.h"
|
||||
#include "Frame.h"
|
||||
|
||||
#include "DlgDropDownList.h"
|
||||
|
||||
DlgDropDownList::DlgDropDownList(DropDownList *ddl, SDL_Rect pos, bool add_scrollbar) : Dialog() {
|
||||
this->ddl = ddl;
|
||||
this->pos = pos;
|
||||
setDefaultPos(false);
|
||||
|
||||
background = SDL_CreateSurface(pos.w, pos.h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000));
|
||||
memset((int *) background->pixels, 0xFFFFFFFF, pos.w * pos.h * 4);
|
||||
surface = SDL_CreateSurface(background->w, background->h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000));
|
||||
|
||||
int listbox_width = pos.w;
|
||||
if (add_scrollbar)
|
||||
listbox_width -= 18;
|
||||
Item *listbox = new Listbox(ddl->getList(), ddl->getListboxItemSize(), getRect(0, 0, listbox_width, ddl->getListboxHeight()));
|
||||
((Listbox *) listbox)->setValueChangedFunc(&listboxValueChangedCallback, (void *)this);
|
||||
|
||||
Item *item = new Frame(getRect(0, 0, listbox_width, pos.h));
|
||||
((Frame *)item)->addItem(listbox);
|
||||
addItem(item);
|
||||
|
||||
if (add_scrollbar) {
|
||||
item = new Scrollbar(0, ddl->getListboxHeight() - pos.h, 0, getRect(pos.w - 18, 0, 18, pos.h));
|
||||
((Scrollbar *) item)->setValueChangedFunc(&scrollbarValueChangedCallback, (void *)this);
|
||||
addItem(item);
|
||||
}
|
||||
|
||||
setExit(false);
|
||||
}
|
||||
|
||||
DlgDropDownList::~DlgDropDownList() {
|
||||
deleteSurfaceAndBackground();
|
||||
deleteItems();
|
||||
}
|
||||
|
||||
void DlgDropDownList::listboxValueChangedCallback(void *p) {
|
||||
((DlgDropDownList *)p)->listboxValueChanged();
|
||||
}
|
||||
|
||||
void DlgDropDownList::listboxValueChanged() {
|
||||
int selected = ((Listbox *)((Frame *) items[0])->getItem(0))->getSelected();
|
||||
ddl->setSelected(selected);
|
||||
setExit(true);
|
||||
}
|
||||
|
||||
void DlgDropDownList::scrollbarValueChangedCallback(void *p) {
|
||||
((DlgDropDownList *)p)->scrollbarValueChanged();
|
||||
}
|
||||
|
||||
void DlgDropDownList::scrollbarValueChanged() {
|
||||
int value = ((Scrollbar *) items[1])->getValue();
|
||||
SDL_Rect *pos = ((Frame *) items[0])->getItem(0)->getPos();
|
||||
pos->y = 0 - value;
|
||||
}
|
||||
19
NewDialogs/DlgDropDownList.h
Normal file
19
NewDialogs/DlgDropDownList.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef DLG_DROP_DOWN_LIST
|
||||
#define DLG_DROP_DOWN_LIST
|
||||
|
||||
#include "Dialog.h"
|
||||
#include "DropDownList.h"
|
||||
|
||||
class DlgDropDownList : public Dialog {
|
||||
private:
|
||||
DropDownList *ddl;
|
||||
public:
|
||||
DlgDropDownList(DropDownList *ddl, SDL_Rect pos, bool add_scrollbar);
|
||||
~DlgDropDownList();
|
||||
static void listboxValueChangedCallback(void *p);
|
||||
void listboxValueChanged();
|
||||
static void scrollbarValueChangedCallback(void *p);
|
||||
void scrollbarValueChanged();
|
||||
};
|
||||
|
||||
#endif
|
||||
287
NewDialogs/DlgMainMenu.cpp
Normal file
287
NewDialogs/DlgMainMenu.cpp
Normal file
@@ -0,0 +1,287 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <SDL3/SDL_storage.h>
|
||||
|
||||
#include "../SDLHelpers.h"
|
||||
|
||||
#include "Item.h"
|
||||
#include "Button.h"
|
||||
#include "Dialog.h"
|
||||
#include "Frame.h"
|
||||
|
||||
#include "DlgMainMenu.h"
|
||||
|
||||
#include "../menu_png.h"
|
||||
|
||||
SDL_EnumerationResult enumStorage(void *userdata, const char *dirname, const char *fname) {
|
||||
SDL_Log("dirname %s fname %s", dirname, fname);
|
||||
SDL_Storage *storage = (SDL_Storage *) ((pair<void *, void *> *)userdata)->first;
|
||||
vector<string> *list = (vector<string> *) ((pair<void *, void *> *)userdata)->second;
|
||||
string path("skin/");
|
||||
path += fname;
|
||||
SDL_PathInfo pi;
|
||||
SDL_GetStoragePathInfo(storage, path.c_str(), &pi);
|
||||
if (pi.type == SDL_PATHTYPE_DIRECTORY) {
|
||||
SDL_Log("is dir");
|
||||
// add name to list if list does not yet have it
|
||||
if (find(list->begin(), list->end(), fname) == list->end()) {
|
||||
list->push_back(fname);
|
||||
}
|
||||
}
|
||||
return SDL_ENUM_CONTINUE;
|
||||
}
|
||||
|
||||
DlgMainMenu::DlgMainMenu() : Dialog() {
|
||||
if (!loadBackground()) {
|
||||
cerr << "Background open failed menu_png! " << SDL_GetError() << endl;
|
||||
}
|
||||
|
||||
vector<string> list;
|
||||
// adding embedded skins
|
||||
list.push_back("Tavallinen");
|
||||
list.push_back("Alppisusi");
|
||||
list.push_back("Chibi");
|
||||
|
||||
SDL_Storage *storage = SDL_OpenTitleStorage(NULL, 0);
|
||||
if (!storage) {
|
||||
SDL_Log("Error opening title storage %s", SDL_GetError());
|
||||
}
|
||||
pair<void *, void *> userdata(storage, &list);
|
||||
if (!SDL_EnumerateStorageDirectory(storage, "skin", &enumStorage, &userdata)) {
|
||||
SDL_Log("Error enumerating dir %s", SDL_GetError());
|
||||
}
|
||||
|
||||
always_on_top_checkbox = new Checkbox("Pysy päällimmäisenä", getColor(0, 0, 0), getRect(16, 24, 200, 12));
|
||||
always_on_top_checkbox->setSelected(1);
|
||||
always_on_top_checkbox->setButtonFunc(&settingsChangedCallback, (void *)this);
|
||||
always_on_top_checkbox->setName("always_on_top");
|
||||
addItem(always_on_top_checkbox);
|
||||
|
||||
enable_sound_checkbox = new Checkbox("Äänet päälle / pois päältä", getColor(0, 0, 0), getRect(16, 40, 200, 12));
|
||||
enable_sound_checkbox->setSelected(1);
|
||||
enable_sound_checkbox->setButtonFunc(&settingsChangedCallback, (void *)this);
|
||||
enable_sound_checkbox->setName("enable_sound");
|
||||
addItem(enable_sound_checkbox);
|
||||
|
||||
open_menu_on_start_checkbox = new Checkbox("Avaa valikko Rouskun käynnistyessä", getColor(0, 0, 0), getRect(16, 56, 200, 12));
|
||||
open_menu_on_start_checkbox->setSelected(1);
|
||||
open_menu_on_start_checkbox->setButtonFunc(&settingsChangedCallback, (void *)this);
|
||||
open_menu_on_start_checkbox->setName("open_menu_on_start");
|
||||
addItem(open_menu_on_start_checkbox);
|
||||
|
||||
run_away_from_mouse_checkbox = new Checkbox("Juokse hiirtä karkuun", getColor(0, 0, 0), getRect(16, 72, 200, 12));
|
||||
run_away_from_mouse_checkbox->setSelected(0);
|
||||
run_away_from_mouse_checkbox->setButtonFunc(&settingsChangedCallback, (void *)this);
|
||||
run_away_from_mouse_checkbox->setName("run_away_from_mouse");
|
||||
addItem(run_away_from_mouse_checkbox);
|
||||
|
||||
skin_list = new DropDownList(list, &dialogs, getRect(16, 104, 200, 25));
|
||||
skin_list->setSelected(0);
|
||||
skin_list->setValueChangedFunc(&settingsChangedCallback, (void *)this);
|
||||
skin_list->setName("skin_list");
|
||||
addItem(skin_list);
|
||||
|
||||
Item *item = new Button("Sulje valikko", getColor(0xB4, 0xC8, 0xB4), getRect(135, 320, 95, 27));
|
||||
((Button *) item)->setButtonFunc(&contCallback, (void *)this);
|
||||
addItem(item);
|
||||
|
||||
item = new Button("Sulje Rousku", getColor(0xC8, 0xB4, 0xB4), getRect(7, 320, 95, 27));
|
||||
((Button *) item)->setButtonFunc(&quitCallback, (void *)this);
|
||||
addItem(item);
|
||||
|
||||
readSettings();
|
||||
setExit(false);
|
||||
if (!open_menu_on_start_checkbox->isSelected()) {
|
||||
setExit(true);
|
||||
}
|
||||
|
||||
app_exit = false;
|
||||
}
|
||||
|
||||
DlgMainMenu::~DlgMainMenu() {
|
||||
deleteSurfaceAndBackground();
|
||||
deleteItems();
|
||||
}
|
||||
|
||||
void DlgMainMenu::draw() {
|
||||
Dialog::draw();
|
||||
|
||||
// Just drawing preview surface on dialog. Create own Item for preview if needed at some point.
|
||||
if (preview_sfe) {
|
||||
SDL_Rect r = { 57, 128, preview_sfe->w, preview_sfe->h };
|
||||
SDL_BlitSurface(preview_sfe, NULL, surface, &r);
|
||||
}
|
||||
|
||||
// We just draw dialogs on this dialog's surface
|
||||
for (unsigned int x = 0; x < dialogs.size(); x++) {
|
||||
SDL_BlitSurface(dialogs[x]->getSurface(), NULL, surface, dialogs[x]->getPos());
|
||||
//items[x]->drawDone();
|
||||
}
|
||||
}
|
||||
|
||||
bool DlgMainMenu::onMousePress(int x, int y) {
|
||||
// we are handling other dialogs as integrated to this dialog so passing our events to them
|
||||
for (unsigned int z = 0; z < dialogs.size(); z++) {
|
||||
dialogs[z]->onMousePress(x - dialogs[z]->getPos()->x, y - dialogs[z]->getPos()->y);
|
||||
if (dialogs[z]->isExit()) {
|
||||
delete dialogs[z];
|
||||
dialogs[z] = NULL;
|
||||
dialogs.erase(dialogs.begin()+z);
|
||||
z--;
|
||||
}
|
||||
}
|
||||
return Dialog::onMousePress(x, y);
|
||||
}
|
||||
|
||||
bool DlgMainMenu::onMouseRelease(int x, int y) {
|
||||
// we are handling other dialogs as integrated to this dialog so passing our events to them
|
||||
for (unsigned int z = 0; z < dialogs.size(); z++) {
|
||||
dialogs[z]->onMouseRelease(x - dialogs[z]->getPos()->x, y - dialogs[z]->getPos()->y);
|
||||
}
|
||||
return Dialog::onMouseRelease(x, y);
|
||||
}
|
||||
|
||||
void DlgMainMenu::readSettings() {
|
||||
SDL_Storage *strg = SDL_OpenUserStorage("TeamSaunakauha", "Rousku", 0);
|
||||
Uint64 file_size;
|
||||
if (!SDL_GetStorageFileSize(strg, "mem.tsk", &file_size)) {
|
||||
SDL_Log("could not get mem.tsk size %s", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
char buf[file_size];
|
||||
if (!SDL_ReadStorageFile(strg, "mem.tsk", buf, file_size)) {
|
||||
SDL_Log("mem.tsk read failed %s", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_CloseStorage(strg);
|
||||
|
||||
string sbuf(buf, file_size);
|
||||
istringstream ssbuf(sbuf);
|
||||
string line;
|
||||
Uint8 lc = 0;
|
||||
while (getline(ssbuf, line)) {
|
||||
switch(lc++) {
|
||||
case 0: always_on_top_checkbox->setSelected(line == "1"); break;
|
||||
case 1: enable_sound_checkbox->setSelected(line == "1"); break;
|
||||
case 2: open_menu_on_start_checkbox->setSelected(line == "1"); break;
|
||||
case 3: run_away_from_mouse_checkbox->setSelected(line == "1"); break;
|
||||
case 4: skin_list->select(line); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DlgMainMenu::writeSettings() {
|
||||
stringstream ssbuf;
|
||||
ssbuf << (always_on_top_checkbox->isSelected() ? 1 : 0) << endl;
|
||||
ssbuf << (enable_sound_checkbox->isSelected() ? 1 : 0) << endl;
|
||||
ssbuf << (open_menu_on_start_checkbox->isSelected() ? 1 : 0) << endl;
|
||||
ssbuf << (run_away_from_mouse_checkbox->isSelected() ? 1 : 0) << endl;
|
||||
ssbuf << skin_list->getString() << endl;
|
||||
|
||||
SDL_Storage *strg = SDL_OpenUserStorage("TeamSaunakauha", "Rousku", 0);
|
||||
if (!SDL_WriteStorageFile(strg, "mem.tsk", ssbuf.str().c_str(), ssbuf.str().length())) {
|
||||
SDL_Log("mem.tsk write failed %s", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
SDL_CloseStorage(strg);
|
||||
}
|
||||
|
||||
void DlgMainMenu::contCallback(void *p, Item *i) {
|
||||
((DlgMainMenu *)p)->cont();
|
||||
}
|
||||
|
||||
void DlgMainMenu::cont() {
|
||||
setExit(true);
|
||||
}
|
||||
|
||||
void DlgMainMenu::quitCallback(void *p, Item *i) {
|
||||
((DlgMainMenu *)p)->quit();
|
||||
}
|
||||
|
||||
void DlgMainMenu::quit() {
|
||||
setAppExit(true);
|
||||
}
|
||||
|
||||
void DlgMainMenu::settingsChangedCallback(void *p, Item *i) {
|
||||
((DlgMainMenu *)p)->settingsChanged(i);
|
||||
}
|
||||
|
||||
void DlgMainMenu::settingsChanged(Item *i) {
|
||||
writeSettings();
|
||||
if (i->getName().length()) {
|
||||
settings_changed_list.push_back(i->getName());
|
||||
}
|
||||
}
|
||||
|
||||
void DlgMainMenu::setAppExit(bool app_exit) {
|
||||
this->app_exit = app_exit;
|
||||
}
|
||||
|
||||
bool DlgMainMenu::isAppExit() {
|
||||
return app_exit;
|
||||
}
|
||||
|
||||
bool DlgMainMenu::isAlwaysOnTop() {
|
||||
return always_on_top_checkbox->isSelected();
|
||||
}
|
||||
|
||||
bool DlgMainMenu::isEnableSound() {
|
||||
return enable_sound_checkbox->isSelected();
|
||||
}
|
||||
|
||||
bool DlgMainMenu::isRunAwayFromMouse() {
|
||||
return run_away_from_mouse_checkbox->isSelected();
|
||||
}
|
||||
|
||||
string DlgMainMenu::getSkin() {
|
||||
return skin_list->getString();
|
||||
}
|
||||
|
||||
string DlgMainMenu::getChangedSetting() {
|
||||
if (settings_changed_list.size()) {
|
||||
string name = settings_changed_list.back();
|
||||
settings_changed_list.pop_back();
|
||||
return name;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void DlgMainMenu::setPreviewSurface(SDL_Surface *preview_sfe) {
|
||||
this->preview_sfe = preview_sfe;
|
||||
}
|
||||
|
||||
bool DlgMainMenu::loadBackground() {
|
||||
SDL_IOStream *io = SDL_IOFromConstMem(menu_png, menu_png_len);
|
||||
if (io == NULL) {
|
||||
SDL_Log("could not open io for menu_png: %s\n", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
background = IMG_Load_IO(io, true);
|
||||
if (background == NULL) {
|
||||
SDL_Log("could not create pet surface: %s\n", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
surface = SDL_CreateSurface(background->w, background->h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000));
|
||||
SDL_Rect d;
|
||||
d.x = 0;
|
||||
d.y = 0;
|
||||
d.w = background->w;
|
||||
d.h = background->h;
|
||||
SDL_SetSurfaceClipRect(surface, &d);
|
||||
pos.w = background->w;
|
||||
pos.h = background->h;
|
||||
return true;
|
||||
}
|
||||
|
||||
55
NewDialogs/DlgMainMenu.h
Normal file
55
NewDialogs/DlgMainMenu.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef DLG_MAIN_MENU
|
||||
#define DLG_MAIN_MENU
|
||||
|
||||
#include "Dialog.h"
|
||||
#include "Button.h"
|
||||
#include "DropDownList.h"
|
||||
#include "Checkbox.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class DlgMainMenu : public Dialog {
|
||||
private:
|
||||
vector<Dialog *> dialogs;
|
||||
|
||||
Checkbox *always_on_top_checkbox;
|
||||
Checkbox *enable_sound_checkbox;
|
||||
Checkbox *open_menu_on_start_checkbox;
|
||||
Checkbox *run_away_from_mouse_checkbox;
|
||||
DropDownList *skin_list;
|
||||
|
||||
bool app_exit;
|
||||
bool show_menu;
|
||||
vector<string> settings_changed_list;
|
||||
|
||||
SDL_Surface *preview_sfe;
|
||||
public:
|
||||
DlgMainMenu();
|
||||
~DlgMainMenu();
|
||||
void draw();
|
||||
bool onMousePress(int x, int y);
|
||||
bool onMouseRelease(int x, int y);
|
||||
void readSettings();
|
||||
void writeSettings();
|
||||
static void contCallback(void *p, Item *i);
|
||||
void cont();
|
||||
static void quitCallback(void *p, Item *i);
|
||||
void quit();
|
||||
static void settingsChangedCallback(void *p, Item *i);
|
||||
void settingsChanged(Item *i);
|
||||
void setAppExit(bool app_exit);
|
||||
bool isAppExit();
|
||||
bool isAlwaysOnTop();
|
||||
bool isEnableSound();
|
||||
bool isRunAwayFromMouse();
|
||||
string getSkin();
|
||||
string getChangedSetting();
|
||||
void setPreviewSurface(SDL_Surface *preview_sfe);
|
||||
bool loadBackground();
|
||||
};
|
||||
|
||||
#endif
|
||||
158
NewDialogs/DropDownList.cpp
Normal file
158
NewDialogs/DropDownList.cpp
Normal file
@@ -0,0 +1,158 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
#include "../SDLHelpers.h"
|
||||
|
||||
#include "DropDownList.h"
|
||||
#include "DlgDropDownList.h"
|
||||
|
||||
DropDownList::DropDownList(vector<string> list, vector<Dialog *> *dialogs, SDL_Rect pos) : Item(pos) {
|
||||
this->list = list;
|
||||
this->dialogs = dialogs;
|
||||
selected = 0;
|
||||
pressed = false;
|
||||
listbox_item_size = 20;
|
||||
value_changed_func = NULL;
|
||||
setSurface(SDL_CreateSurface(getPos()->w, getPos()->h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)));
|
||||
drawDropDownList();
|
||||
}
|
||||
|
||||
DropDownList::~DropDownList() {
|
||||
SDL_DestroySurface(getSurface());
|
||||
}
|
||||
|
||||
void DropDownList::drawDropDownList() {
|
||||
listbox_height = list.size() * listbox_item_size;
|
||||
|
||||
SDL_Surface *surface = getSurface();
|
||||
int w = getPos()->w;
|
||||
int h = getPos()->h;
|
||||
Uint32 *p = (Uint32 *) surface->pixels;
|
||||
SDL_Color color;
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
if (y == 0 || y == h-1 || x == 0 || x == w-1 || x == w-18) {
|
||||
color.r = 0;
|
||||
color.g = 0;
|
||||
color.b = 0;
|
||||
} else {
|
||||
color.r = 255;
|
||||
color.g = 255;
|
||||
color.b = 255;
|
||||
}
|
||||
*p = SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, color.r, color.g, color.b);
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
//drawing arrow
|
||||
p = (Uint32 *) surface->pixels;
|
||||
int posx = w - 13;
|
||||
int posy = h / 2 - 2;
|
||||
for (int y = 0; y < 4; y++) {
|
||||
for (int x = 0; x < 8 - y * 2; x++) {
|
||||
p[(posy + y) * w + (posx + x + y)] = SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, color.r, color.g, color.b);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Color text_color = {0, 0, 0};
|
||||
vector<string> fonts;
|
||||
fonts.push_back("ariblk.ttf");
|
||||
fonts.push_back("FreeSans.ttf");
|
||||
fonts.push_back("DejaVuSans.ttf");
|
||||
fonts.push_back("arial.ttf");
|
||||
TTF_Font *font = openFont(fonts, 14);
|
||||
SDL_Surface *text_surface;
|
||||
if (!font)
|
||||
cout << "TTF_OpenFont is broken!" << endl;
|
||||
if(!(text_surface = TTF_RenderText_Blended(font, list[selected].c_str(), list[selected].length(), text_color)))
|
||||
cout << SDL_GetError() << endl;
|
||||
SDL_Rect pos;
|
||||
pos.x = 8;
|
||||
pos.y = surface->h / 2 - text_surface->h / 2;
|
||||
SDL_BlitSurface(text_surface, NULL, surface, &pos);
|
||||
SDL_DestroySurface(text_surface);
|
||||
TTF_CloseFont(font);
|
||||
}
|
||||
|
||||
bool DropDownList::onMousePress(int x, int y) {
|
||||
bool is_in_x = x > getPos()->x && x < getPos()->x + getPos()->w;
|
||||
bool is_in_y = y > getPos()->y && y < getPos()->y + getPos()->h;
|
||||
if (is_in_x && is_in_y) {
|
||||
//setSurface(buttonDown);
|
||||
pressed = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void DropDownList::onMouseRelease(int x, int y) {
|
||||
bool is_in_x = x > getPos()->x && x < getPos()->x + getPos()->w;
|
||||
bool is_in_y = y > getPos()->y && y < getPos()->y + getPos()->h;
|
||||
if (is_in_x && is_in_y && pressed) {
|
||||
int height;
|
||||
bool add_scrollbar;
|
||||
if (listbox_height < 200) {
|
||||
height = listbox_height;
|
||||
add_scrollbar = false;
|
||||
} else {
|
||||
height = 200;
|
||||
add_scrollbar = true;
|
||||
}
|
||||
dialogs->push_back(new DlgDropDownList(this, getRect(getPos()->x, getPos()->y + getPos()->h, getPos()->w, height), add_scrollbar));
|
||||
}
|
||||
//setSurface(buttonUp);
|
||||
pressed = false;
|
||||
}
|
||||
|
||||
vector<string> DropDownList::getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
void DropDownList::setSelected(int selected) {
|
||||
this->selected = selected;
|
||||
if (value_changed_func) {
|
||||
(*value_changed_func)(p, this);
|
||||
}
|
||||
drawDropDownList();
|
||||
}
|
||||
|
||||
void DropDownList::setValueChangedFunc(void (*value_changed_func)(void *p, Item *i), void *p) {
|
||||
this->value_changed_func = value_changed_func;
|
||||
this->p = p;
|
||||
}
|
||||
|
||||
string DropDownList::getString() {
|
||||
return list[selected];
|
||||
}
|
||||
|
||||
void DropDownList::reset() {
|
||||
list.clear();
|
||||
}
|
||||
|
||||
void DropDownList::add(string s) {
|
||||
list.push_back(s);
|
||||
}
|
||||
|
||||
bool DropDownList::select(string s) {
|
||||
for (int x = 0; x < list.size(); x++) {
|
||||
if (list[x].compare(s) == 0) {
|
||||
setSelected(x);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int DropDownList::getListboxItemSize() {
|
||||
return listbox_item_size;
|
||||
}
|
||||
|
||||
int DropDownList::getListboxHeight() {
|
||||
return listbox_height;
|
||||
}
|
||||
40
NewDialogs/DropDownList.h
Normal file
40
NewDialogs/DropDownList.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef DROP_DOWN_LIST
|
||||
#define DROP_DOWN_LIST
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "Item.h"
|
||||
#include "Dialog.h"
|
||||
|
||||
class DropDownList : public Item {
|
||||
private:
|
||||
vector<string> list;
|
||||
vector<Dialog *> *dialogs;
|
||||
int selected;
|
||||
int listbox_item_size;
|
||||
int listbox_height;
|
||||
bool pressed;
|
||||
void (*value_changed_func)(void *p, Item *i);
|
||||
void *p;
|
||||
public:
|
||||
DropDownList(vector<string> list, vector<Dialog *> *dialogs, SDL_Rect pos);
|
||||
~DropDownList();
|
||||
void drawDropDownList();
|
||||
bool onMousePress(int x, int y);
|
||||
void onMouseRelease(int x, int y);
|
||||
vector<string> getList();
|
||||
void setSelected(int selected);
|
||||
void setValueChangedFunc(void (*value_changed_func)(void *p, Item *i), void *p);
|
||||
string getString();
|
||||
void reset();
|
||||
void add(string s);
|
||||
bool select(string s);
|
||||
int getListboxItemSize();
|
||||
int getListboxHeight();
|
||||
};
|
||||
|
||||
#endif
|
||||
65
NewDialogs/Frame.cpp
Normal file
65
NewDialogs/Frame.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "Frame.h"
|
||||
|
||||
Frame::Frame(SDL_Rect pos) : Item(pos) {
|
||||
setSurface(SDL_CreateSurface(getPos()->w, getPos()->h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)));
|
||||
memset((int *) getSurface()->pixels, 0xFFFFFFFF, pos.w * pos.h * 4);
|
||||
}
|
||||
|
||||
Frame::~Frame() {
|
||||
SDL_DestroySurface(getSurface());
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
delete items[z];
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Surface *Frame::getSurface() {
|
||||
draw();
|
||||
return surface;
|
||||
}
|
||||
|
||||
void Frame::draw() {
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
SDL_Rect src = *items[z]->getPos();
|
||||
SDL_Rect dst = {0,0,0,0};
|
||||
if (src.x < 0) {
|
||||
src.x *= -1;
|
||||
} else {
|
||||
dst.x = src.x;
|
||||
src.x = 0;
|
||||
}
|
||||
if (src.y < 0) {
|
||||
src.y *= -1;
|
||||
} else {
|
||||
dst.y = src.y;
|
||||
src.y = 0;
|
||||
}
|
||||
SDL_BlitSurface(items[z]->getSurface(), &src, surface, &dst);
|
||||
}
|
||||
}
|
||||
|
||||
void Frame::addItem(Item *item) {
|
||||
items.push_back(item);
|
||||
}
|
||||
|
||||
Item *Frame::getItem(int i) {
|
||||
return items[i];
|
||||
}
|
||||
|
||||
bool Frame::onMousePress(int x, int y) {
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
items[z]->onMousePress(x - getPos()->x, y - getPos()->y);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Frame::onMouseRelease(int x, int y) {
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
items[z]->onMouseRelease(x - getPos()->x, y - getPos()->y);
|
||||
}
|
||||
}
|
||||
|
||||
void Frame::onMouseMove(int x, int y) {
|
||||
for (unsigned int z = 0; z < items.size(); z++) {
|
||||
items[z]->onMouseMove(x - getPos()->x, y - getPos()->y);
|
||||
}
|
||||
}
|
||||
27
NewDialogs/Frame.h
Normal file
27
NewDialogs/Frame.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef FRAME
|
||||
#define FRAME
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "Item.h"
|
||||
|
||||
class Frame : public Item {
|
||||
protected:
|
||||
vector<Item *> items;
|
||||
public:
|
||||
Frame(SDL_Rect pos);
|
||||
~Frame();
|
||||
SDL_Surface *getSurface();
|
||||
void draw();
|
||||
void addItem(Item *item);
|
||||
Item *getItem(int i);
|
||||
bool onMousePress(int x, int y);
|
||||
void onMouseRelease(int x, int y);
|
||||
void onMouseMove(int x, int y);
|
||||
};
|
||||
|
||||
#endif
|
||||
58
NewDialogs/Item.cpp
Normal file
58
NewDialogs/Item.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "Item.h"
|
||||
|
||||
Item::Item(SDL_Rect pos) {
|
||||
this->pos = pos;
|
||||
needs_redraw = true;
|
||||
}
|
||||
|
||||
Item::~Item() {
|
||||
}
|
||||
|
||||
SDL_Surface *Item::getSurface() {
|
||||
return surface;
|
||||
}
|
||||
|
||||
void Item::setSurface(SDL_Surface *surface) {
|
||||
needs_redraw = true;
|
||||
this->surface = surface;
|
||||
}
|
||||
|
||||
void Item::setPos(SDL_Rect pos) {
|
||||
this->pos = pos;
|
||||
}
|
||||
|
||||
SDL_Rect *Item::getPos() {
|
||||
return &pos;
|
||||
}
|
||||
|
||||
bool Item::onMousePress(int x, int y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void Item::onMouseRelease(int x, int y) {
|
||||
}
|
||||
|
||||
void Item::onMouseMove(int x, int y) {
|
||||
}
|
||||
|
||||
void Item::onFingerDown(int x, int y, int finger_id) {
|
||||
}
|
||||
|
||||
void Item::onFingerUp(int x, int y, int finger_id) {
|
||||
}
|
||||
|
||||
bool Item::needsRedraw() {
|
||||
return needs_redraw;
|
||||
}
|
||||
|
||||
void Item::drawDone() {
|
||||
needs_redraw = false;
|
||||
}
|
||||
|
||||
void Item::setName(string name) {
|
||||
this->name = name;
|
||||
}
|
||||
|
||||
string Item::getName() {
|
||||
return name;
|
||||
}
|
||||
35
NewDialogs/Item.h
Normal file
35
NewDialogs/Item.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef ITEM_H
|
||||
#define ITEM_H
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Item {
|
||||
protected:
|
||||
SDL_Surface *surface;
|
||||
bool needs_redraw;
|
||||
private:
|
||||
SDL_Rect pos;
|
||||
string name;
|
||||
public:
|
||||
Item(SDL_Rect pos);
|
||||
virtual ~Item();
|
||||
virtual SDL_Surface *getSurface();
|
||||
void setSurface(SDL_Surface *surface);
|
||||
void setPos(SDL_Rect pos);
|
||||
SDL_Rect *getPos();
|
||||
virtual bool onMousePress(int x, int y);
|
||||
virtual void onMouseRelease(int x, int y);
|
||||
virtual void onMouseMove(int x, int y);
|
||||
virtual void onFingerDown(int x, int y, int finger_id);
|
||||
virtual void onFingerUp(int x, int y, int finger_id);
|
||||
bool needsRedraw();
|
||||
void drawDone();
|
||||
void setName(string name);
|
||||
string getName();
|
||||
};
|
||||
|
||||
#endif
|
||||
91
NewDialogs/Listbox.cpp
Normal file
91
NewDialogs/Listbox.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
|
||||
#include "../SDLHelpers.h"
|
||||
|
||||
#include "Listbox.h"
|
||||
|
||||
Listbox::Listbox(vector<string> list, int item_size, SDL_Rect pos) : Item(pos) {
|
||||
this->list = list;
|
||||
this->item_size = item_size;
|
||||
selected = 1;
|
||||
setSurface(SDL_CreateSurface(getPos()->w, getPos()->h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)));
|
||||
drawListbox();
|
||||
}
|
||||
|
||||
Listbox::~Listbox() {
|
||||
SDL_DestroySurface(getSurface());
|
||||
}
|
||||
|
||||
void Listbox::drawListbox() {
|
||||
SDL_Surface *surface = getSurface();
|
||||
int w = getPos()->w;
|
||||
int h = getPos()->h;
|
||||
|
||||
Uint32 *p = (Uint32 *) surface->pixels;
|
||||
SDL_Color color;
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
if (y == 0 || y == h-1 || x == 0 || x == w-1) {
|
||||
color.r = 0;
|
||||
color.g = 0;
|
||||
color.b = 0;
|
||||
} else {
|
||||
color.r = 255;
|
||||
color.g = 255;
|
||||
color.b = 255;
|
||||
}
|
||||
*p = SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, color.r, color.g, color.b);
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
vector<string> fonts;
|
||||
fonts.push_back("ariblk.ttf");
|
||||
fonts.push_back("FreeSans.ttf");
|
||||
fonts.push_back("DejaVuSans.ttf");
|
||||
TTF_Font *font = openFont(fonts, 14);
|
||||
if (!font) {
|
||||
cout << "TTF_OpenFont is broken!" << endl;
|
||||
} else {
|
||||
for (int x = 0; x < list.size(); x++) {
|
||||
SDL_Color text_color = {0, 0, 0};
|
||||
SDL_Surface *text_surface;
|
||||
if(!(text_surface = TTF_RenderText_Blended(font, list[x].c_str(), list[x].length(), text_color))) {
|
||||
cout << SDL_GetError() << endl;
|
||||
}
|
||||
SDL_Rect pos;
|
||||
pos.x = 8;
|
||||
pos.y = x * item_size;
|
||||
SDL_BlitSurface(text_surface, NULL, surface, &pos);
|
||||
SDL_DestroySurface(text_surface);
|
||||
}
|
||||
TTF_CloseFont(font);
|
||||
}
|
||||
}
|
||||
|
||||
bool Listbox::onMousePress(int x, int y) {
|
||||
bool is_in_x = x > getPos()->x && x < getPos()->x + getPos()->w;
|
||||
bool is_in_y = y > getPos()->y && y < getPos()->y + getPos()->h;
|
||||
if (is_in_x && is_in_y) {
|
||||
selected = (y - getPos()->y) / item_size;
|
||||
if (value_changed_func) {
|
||||
(*value_changed_func)(p);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Listbox::setValueChangedFunc(void (*value_changed_func)(void *p), void *p) {
|
||||
this->value_changed_func = value_changed_func;
|
||||
this->p = p;
|
||||
}
|
||||
|
||||
int Listbox::getSelected() {
|
||||
return selected;
|
||||
}
|
||||
26
NewDialogs/Listbox.h
Normal file
26
NewDialogs/Listbox.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef LISTBOX
|
||||
#define LISTBOX
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "Item.h"
|
||||
|
||||
class Listbox : public Item {
|
||||
private:
|
||||
vector<string> list;
|
||||
int selected;
|
||||
int item_size;
|
||||
void (*value_changed_func)(void *p);
|
||||
void *p;
|
||||
public:
|
||||
Listbox(vector<string> list, int item_size, SDL_Rect pos);
|
||||
~Listbox();
|
||||
void drawListbox();
|
||||
bool onMousePress(int x, int y);
|
||||
void setValueChangedFunc(void (*value_changed_func)(void *p), void *p);
|
||||
int getSelected();
|
||||
};
|
||||
|
||||
#endif
|
||||
103
NewDialogs/Scrollbar.cpp
Normal file
103
NewDialogs/Scrollbar.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#include "Scrollbar.h"
|
||||
|
||||
Scrollbar::Scrollbar(int min_value, int max_value, int value, SDL_Rect pos) : Item(pos) {
|
||||
vertical = (pos.h > pos.w);
|
||||
this->min_value = min_value;
|
||||
this->max_value = max_value;
|
||||
this->value = value;
|
||||
bar_size = 18;
|
||||
pressed = false;
|
||||
value_changed_func = NULL;
|
||||
setSurface(SDL_CreateSurface(getPos()->w, getPos()->h, SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)));
|
||||
drawScrollbar();
|
||||
}
|
||||
|
||||
Scrollbar::~Scrollbar() {
|
||||
SDL_DestroySurface(getSurface());
|
||||
}
|
||||
|
||||
void Scrollbar::drawScrollbar() {
|
||||
SDL_Surface *surface = getSurface();
|
||||
int w = getPos()->w;
|
||||
int h = getPos()->h;
|
||||
|
||||
Uint32 *p = (Uint32 *) surface->pixels;
|
||||
SDL_Color color;
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
int x_or_y = (vertical ? y : x);
|
||||
bool bar_pos_test = (x_or_y > getBarPos() && x_or_y < getBarPos() + bar_size);
|
||||
|
||||
if (y == 0 || y == h-1 || x == 0 || x == w-1 || bar_pos_test) {
|
||||
color.r = 0;
|
||||
color.g = 0;
|
||||
color.b = 0;
|
||||
} else {
|
||||
color.r = 255;
|
||||
color.g = 255;
|
||||
color.b = 255;
|
||||
}
|
||||
*p = SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, color.r, color.g, color.b);
|
||||
p++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Scrollbar::onMousePress(int x, int y) {
|
||||
bool is_in_x = x > getPos()->x && x < getPos()->x + getPos()->w;
|
||||
bool is_in_y = y > getPos()->y && y < getPos()->y + getPos()->h;
|
||||
if (is_in_x && is_in_y) {
|
||||
//setSurface(buttonDown);
|
||||
mouse_offset = (vertical ? y : x) - getBarPos();
|
||||
pressed = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Scrollbar::onMouseRelease(int x, int y) {
|
||||
//bool is_in_x = x > getPos()->x && x < getPos()->x + getPos()->w;
|
||||
//bool is_in_y = y > getPos()->y && y < getPos()->y + getPos()->h;
|
||||
//if (is_in_x && is_in_y && pressed) {
|
||||
if (pressed) {
|
||||
setValueByBarPos((vertical ? y : x) - mouse_offset);
|
||||
drawScrollbar();
|
||||
if (value_changed_func) {
|
||||
(*value_changed_func)(p);
|
||||
}
|
||||
}
|
||||
//setSurface(buttonUp);
|
||||
pressed = false;
|
||||
}
|
||||
|
||||
void Scrollbar::onMouseMove(int x, int y) {
|
||||
if (pressed) {
|
||||
setValueByBarPos((vertical ? y : x) - mouse_offset);
|
||||
drawScrollbar();
|
||||
if (value_changed_func)
|
||||
(*value_changed_func)(p);
|
||||
}
|
||||
}
|
||||
|
||||
int Scrollbar::getBarPos() {
|
||||
int w_or_h = (vertical ? getPos()->h : getPos()->w);
|
||||
return (w_or_h - bar_size) * (value - min_value) / (max_value - min_value);
|
||||
}
|
||||
|
||||
void Scrollbar::setValueByBarPos(int pos) {
|
||||
int w_or_h = (vertical ? getPos()->h : getPos()->w);
|
||||
value = (max_value - min_value) * pos / (w_or_h - bar_size) + min_value;
|
||||
if (value < min_value)
|
||||
value = min_value;
|
||||
if (value > max_value)
|
||||
value = max_value;
|
||||
}
|
||||
|
||||
void Scrollbar::setValueChangedFunc(void (*value_changed_func)(void *p), void *p) {
|
||||
this->value_changed_func = value_changed_func;
|
||||
this->p = p;
|
||||
}
|
||||
|
||||
int Scrollbar::getValue() {
|
||||
return value;
|
||||
}
|
||||
32
NewDialogs/Scrollbar.h
Normal file
32
NewDialogs/Scrollbar.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef SCROLLBAR
|
||||
#define SCROLLBAR
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "Item.h"
|
||||
|
||||
class Scrollbar : public Item {
|
||||
private:
|
||||
bool vertical;
|
||||
int min_value;
|
||||
int max_value;
|
||||
int value;
|
||||
int bar_size;
|
||||
int mouse_offset;
|
||||
bool pressed;
|
||||
void (*value_changed_func)(void *p);
|
||||
void *p;
|
||||
public:
|
||||
Scrollbar(int min_value, int max_value, int value, SDL_Rect pos);
|
||||
~Scrollbar();
|
||||
void drawScrollbar();
|
||||
bool onMousePress(int x, int y);
|
||||
void onMouseRelease(int x, int y);
|
||||
void onMouseMove(int x, int y);
|
||||
int getBarPos();
|
||||
void setValueByBarPos(int pos);
|
||||
void setValueChangedFunc(void (*value_changed_func)(void *p), void *p);
|
||||
int getValue();
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user