gcores ~/apps/Menu branch first commit

This commit is contained in:
cpi
2019-07-16 03:40:14 +00:00
parent 2d75a8d65d
commit 45737f787c
2136 changed files with 214 additions and 17128 deletions

View File

@@ -1,52 +0,0 @@
/*
set_text(render void, text. char, color uint32)
*/
class game+, control+, button {
inherit game::control = super;
private import lib::sdl2;
struct button {
tex texture;
}
func constructor() {
constructor(this);
}
func destructor() {
destructor(this);
}
inline set_text(render void, text. char, color uint32 = 0xff000000) {
this.tex->init(
global::uni_font->create_tex(render, text, color));
}
func draw(render void) {
var rect SDL_Rect = this->get_rect();
SDL_SetRenderDrawColor(render, 245, 245, 245, 0xff);
SDL_RenderFillRect(render, &rect);
if this->has_focus() {
SDL_SetRenderDrawColor(render, 0, 162, 232, 0xff);
else
SDL_SetRenderDrawColor(render, 127, 127, 127, 0xff);
}
SDL_RenderDrawRect(render, &rect);
if this.tex->get_texture() ~= null {
var width int = this.tex->get_width();
var height int = this.tex->get_height();
rect.x++;
rect.y++;
rect.w -= 2;
rect.h -= 2;
this.tex->draw2(render,
rect.x + (rect.w - width) / 2,
rect.y + (rect.h - height) / 2, width, height);
}
}
}

View File

@@ -1,75 +0,0 @@
/*
set_text_color(color uint32)
set_text(render void, text. char, color uint32)
get_text() .char
get_keybd_cb() void
*/
class game+, control+, edit {
inherit game::control = super;
private import lib::sdl2;
private alias string = std::string;
struct edit {
tex texture;
str string;
text_color uint32;
}
func constructor() {
constructor(this);
this.text_color = 0xff000000;
}
func destructor() {
destructor(this);
}
inline set_text_color(color uint32) {
this.text_color = color;
}
inline set_text(render void, text. char) {
this.str->set(text);
this.tex->init(
global::uni_font->create_tex(render, text, this.text_color));
}
inline get_text() .char {
return this.str->ptr();
}
inline get_keybd_cb() void {
return lambda(text. char, this.edit) [
this->set_text(this->get_window()->get_render(), text);
];
}
func draw(render void) {
define {
TEXT_GAP = 3;
}
var rect SDL_Rect = this->get_rect();
SDL_SetRenderDrawColor(render, 0xff, 0xff, 0xff, 0xff);
SDL_RenderFillRect(render, &rect);
if this->has_focus() {
SDL_SetRenderDrawColor(render, 0, 162, 232, 0xff);
else
SDL_SetRenderDrawColor(render, 127, 127, 127, 0xff);
}
SDL_RenderDrawRect(render, &rect);
if this.tex->get_texture() ~= null {
var width int = this.tex->get_width();
var height int = this.tex->get_height();
var max_width int = rect.w - (TEXT_GAP * 2);
if width > max_width;
width = max_width;
this.tex->draw2(render, rect.x + TEXT_GAP, rect.y + (rect.h - height) / 2, width, height);
}
}
}

View File

@@ -1,79 +0,0 @@
/*
set_normal_image(render void, filename. char) bool
set_hover_image(render void, filename. char) bool
set_image(render void, filename. char) bool
set_outline_size(size int)
get_outline_size() int
*/
class game+, control+, image_button {
inherit game::control = super;
private import lib::sdl2;
struct image_button {
normal texture;
hover texture;
image texture;
outline_size int;
}
func constructor() {
constructor(this);
}
func destructor() {
destructor(this);
}
inline set_normal_image(render void, filename. char) bool {
if func = this.normal->load_file(render, filename) {
this->set_size(
this.normal->get_width(),
this.normal->get_height());
}
}
inline set_hover_image(render void, filename. char) bool {
return this.hover->load_file(render, filename);
}
inline set_image(render void, filename. char) bool {
return this.image->load_file(render, filename);
}
inline set_outline_size(size int) {
this.outline_size = size;
}
inline get_outline_size() int {
return this.outline_size;
}
func draw(render void) {
var rect SDL_Rect = this->get_rect();
if this->has_focus() {
this.hover->draw(render, rect.x, rect.y);
else
this.normal->draw(render, rect.x, rect.y);
}
if this.outline_size {
rect.x += this.outline_size;
rect.y += this.outline_size;
rect.w -= this.outline_size * 2;
rect.h -= this.outline_size * 2;
}
var width int = this.image->get_width();
var height int = this.image->get_height();
if width > rect.w;
width = rect.w;
if height > rect.h;
height = rect.h;
this.image->draw3(render,
rect.x + (rect.w - width) / 2,
rect.y + (rect.h - height) / 2, width, height);
}
}

View File

@@ -1,35 +0,0 @@
/*
set_text(render void, text. char, color uint32) bool
*/
class game+, control+, label {
inherit game::control = super;
private import lib::sdl2;
struct label {
tex texture;
}
func constructor() {
constructor(this); }
func destructor() {
destructor(this); }
func set_text(render void, text. char, color uint32 = 0xff000000) bool {
if func = this.tex->init(global::uni_font->create_tex(render, text, color)) {
this->set_size(
this.tex->get_width(),
this.tex->get_height());
}
}
func draw(render void) {
this.tex->draw(render,
this->get_x(),
this->get_y());
}
}

View File

@@ -1,158 +0,0 @@
/*
set_callback(on_select void, on_close void, arg void)
add_menu(render void, id int, title. char)
*/
class game+, control+, popup_menu {
inherit game::control = super;
private import rt::c, lib::sdl2;
private alias
string = std::string,
vector = std::vector;
class menu {
struct menu {
white_title texture;
black_title texture;
id int;
}
func constructor() {
constructor(this);
this.id = -1;
}
func destructor() {
destructor(this);
}
}
struct popup_menu {
menus vector<menu>;
input_delay repeat;
input_lock norepeat;
on_select void;
on_close void;
arg void;
cursor int;
}
func constructor() {
memset(this, 0, sizeof(popup_menu));
constructor(this);
}
func destructor() {
destructor(this);
}
define {
MENU_GAP = 12;
MENU_HEIGHT = 25;
}
func init(render void) bool {
this.input_delay->init(300, -10, 20);
return true;
}
/*
on_select = lambda(id int, arg void) [
];
on_close = lambda(arg void) [
];
*/
inline set_callback(on_select void, on_close void, arg void) {
this.on_select = on_select;
this.on_close = on_close;
this.arg = arg;
}
func add_menu(render void, id int, title. char) {
var menu. menu = this.menus->new();
menu.white_title->init(
global::uni_font->create_tex(render, title, 0xffffffff));
menu.black_title->init(
global::uni_font->create_tex(render, title, 0xff000000));
menu.id = id;
var width int = menu.black_title->get_width() + (MENU_GAP * 2);
if this->get_width() < width;
this->set_width(width);
this->set_height(this.menus->count() * MENU_HEIGHT + 2);
}
func update(render void) {
var state. uint8 = SDL_GetKeyboardState(null);
if state[CPI_SCANCODE_UP] {
if this.input_delay->do() {
if this.cursor;
this.cursor--;
this.input_delay->undo();
}
elseif state[CPI_SCANCODE_DOWN];
if this.input_delay->do() {
if this.cursor < this.menus->count() - 1;
this.cursor++;
this.input_delay->undo();
}
else
this.input_delay->reset();
}
if state[CPI_SCANCODE_A] or
state[CPI_SCANCODE_B] or
state[CPI_SCANCODE_MENU] {
if this.input_lock->do() {
if state[CPI_SCANCODE_B] {
proto fn(id int, arg void);
fn[this.on_select](this.menus->at(this.cursor).id, this.arg);
}
{
proto fn(arg void);
fn[this.on_close](this.arg);
}
}
else
this.input_lock->set(false);
}
}
func draw(render void) {
var rect SDL_Rect = this->get_rect();
SDL_SetRenderDrawColor(render, 0xff, 0xff, 0xff, 0xff);
SDL_RenderFillRect(render, &rect);
SDL_SetRenderDrawColor(render, 0xcd, 0xcd, 0xcd, 0xff);
SDL_RenderDrawRect(render, &rect);
rect.x += 1;
rect.y += 1;
rect.w -= 2;
rect.h -= 2;
forvar index size_t = 0, count size_t = this.menus->count(); index < count; index++ {
var menu. menu = this.menus->at(index);
if this.cursor == index {
var sub_rect SDL_Rect;
sub_rect.x = rect.x;
sub_rect.y = rect.y;
sub_rect.w = rect.w;
sub_rect.h = MENU_HEIGHT;
SDL_SetRenderDrawColor(render, 0x00, 0xa2, 0xe8, 0xff);
SDL_RenderFillRect(render, &sub_rect);
menu.white_title->draw(render, rect.x + MENU_GAP,
rect.y + (MENU_HEIGHT - menu.white_title->get_height()) / 2);
else
menu.black_title->draw(render, rect.x + MENU_GAP,
rect.y + (MENU_HEIGHT - menu.black_title->get_height()) / 2);
}
rect.y += MENU_HEIGHT;
}
}
func wake() {
this.input_delay->reset(false);
this.input_lock->set(true);
}
}

View File

@@ -1,38 +0,0 @@
// template
class game+, control+, temp {
inherit game::control = super;
private import lib::sdl2;
struct temp {
}
func constructor() {
constructor(this);
}
func destructor() {
destructor(this);
}
func init(render void) bool {
return true;
}
func update(render void) {
this.super->update(render);
}
func draw(render void) {
this.super->draw(render);
}
func wake() {
this.super->wake();
}
}