Skip to content

Commit

Permalink
Initial approach to solve the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dodomorandi committed Apr 26, 2020
1 parent 8c1a495 commit 51656f0
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 17 deletions.
8 changes: 4 additions & 4 deletions data/pigui/modules/system-view-ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ local function showDvLine(leftIcon, resetIcon, rightIcon, key, Formatter, leftTo
end
end
end
local press = ui.coloredSelectedIconButton(leftIcon, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_BACK, svColor.BUTTON_INK, leftTooltip)
if press or (key ~= "factor" and ui.isItemActive()) then
local press, _, held = ui.coloredSelectedIconButtonPressedHoveredHeld(leftIcon, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_BACK, svColor.BUTTON_INK, leftTooltip, nil, key)
if press or (key ~= "factor" and (held or ui.isItemActive())) then
systemView:TransferPlannerAdd(key, -10)
end
wheel()
Expand All @@ -83,8 +83,8 @@ local function showDvLine(leftIcon, resetIcon, rightIcon, key, Formatter, leftTo
end
wheel()
ui.sameLine()
press = ui.coloredSelectedIconButton(rightIcon, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_BACK, svColor.BUTTON_INK, rightTooltip)
if press or (key ~= "factor" and ui.isItemActive()) then
press, _, held = ui.coloredSelectedIconButtonPressedHoveredHeld(rightIcon, mainButtonSize, false, mainButtonFramePadding, svColor.BUTTON_BACK, svColor.BUTTON_INK, rightTooltip, nil, key)
if press or (key ~= "factor" and (held or ui.isItemActive())) then
systemView:TransferPlannerAdd(key, 10)
end
wheel()
Expand Down
10 changes: 7 additions & 3 deletions data/pigui/pigui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ ui.coloredSelectedButton = function(label, thesize, is_selected, bg_color, toolt
end
return res
end
ui.coloredSelectedIconButton = function(icon, thesize, is_selected, frame_padding, bg_color, fg_color, tooltip, img_size)
ui.coloredSelectedIconButtonPressedHoveredHeld = function(icon, thesize, is_selected, frame_padding, bg_color, fg_color, tooltip, img_size, id_suffix)
if is_selected then
pigui.PushStyleColor("Button", bg_color)
pigui.PushStyleColor("ButtonHovered", bg_color:tint(0.1))
Expand All @@ -825,13 +825,17 @@ ui.coloredSelectedIconButton = function(icon, thesize, is_selected, frame_paddin
end
local uv0,uv1 = get_icon_tex_coords(icon)
pigui.PushID(tooltip)
local res = pigui.ButtonImageSized(ui.icons_texture, thesize, img_size or Vector2(0,0), uv0, uv1, frame_padding, ui.theme.colors.lightBlueBackground, fg_color)
local pressed, hovered, held = pigui.ButtonImageSized(ui.icons_texture, thesize, img_size or Vector2(0,0), uv0, uv1, frame_padding, ui.theme.colors.lightBlueBackground, fg_color, id_suffix)
pigui.PopID()
pigui.PopStyleColor(3)
if pigui.IsItemHovered() then
pigui.SetTooltip(tooltip)
end
return res
return pressed, hovered, held
end
ui.coloredSelectedIconButton = function(icon, thesize, is_selected, frame_padding, bg_color, fg_color, tooltip, img_size, id_suffix)
local pressed, hovered, held = ui.coloredSelectedIconButtonPressedHoveredHeld(icon, thesize, is_selected, frame_padding, bg_color, fg_color, tooltip, img_size, id_suffix)
return pressed
end

local gauge_show_percent = true
Expand Down
185 changes: 185 additions & 0 deletions src/Optional.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#ifndef _OPTIONAL_H
#define _OPTIONAL_H

#include <type_traits>
#include <utility>

template <typename T>
struct Optional {
using value_type = T;

Optional() noexcept :
populated(false)
{}

Optional(const Optional<T> &other) noexcept(std::is_nothrow_copy_constructible<T>::value) :
populated(other.populated)
{
if (populated) {
new (&content.t) T(other.content.t);
}
}

Optional(Optional<T> &&other) noexcept(
std::is_nothrow_move_constructible<T>::value &&
std::is_nothrow_destructible<T>::value) :
populated(other.populated)
{
if (populated) {
new (&content.t) T(std::move(other.content.t));
other.content.t.~T();
other.populated = false;
}
}

Optional(const T &t) noexcept(std::is_nothrow_copy_constructible<T>::value) :
populated(true)
{
new (&content.t) T(t);
}

Optional(T &&t) noexcept(std::is_nothrow_move_constructible<T>::value) :
populated(true)
{
new (&content.t) T(std::move(t));
}

~Optional() noexcept(std::is_nothrow_destructible<T>::value)
{
if (populated) {
content.t.~T();
}
}

Optional &operator=(const Optional<T> &other) noexcept(
std::is_nothrow_copy_constructible<T>::value &&
std::is_nothrow_copy_assignable<T>::value &&
std::is_nothrow_destructible<T>::value)
{
if (other.populated) {
if (populated) {
content.t = other.content.t;
} else {
new (&content.t) T(other.content.t);
populated = true;
}
} else if (populated) {
content.t.~T();
populated = false;
}

return *this;
}

Optional &operator=(Optional<T> &&other) noexcept(
std::is_nothrow_move_constructible<T>::value &&
std::is_nothrow_move_assignable<T>::value &&
std::is_nothrow_destructible<T>::value)
{
if (other.populated) {
if (populated) {
content.t = std::move(other.content.t);
other.content.t.~T();
other.populated = false;
} else {
new (&content.t) T(std::move(other.content.t));
other.content.t.~T();
other.populated = false;
populated = true;
}
} else if (populated) {
content.t.~T();
populated = false;
}

return *this;
}

Optional &operator=(const T &other) noexcept(
std::is_nothrow_copy_constructible<T>::value &&
std::is_nothrow_copy_assignable<T>::value &&
std::is_nothrow_destructible<T>::value)
{
if (populated) {
content.t = other;
} else {
new (&content.t) T(other);
populated = true;
}

return *this;
}

Optional &operator=(T &&other) noexcept(
std::is_nothrow_move_constructible<T>::value &&
std::is_nothrow_move_assignable<T>::value &&
std::is_nothrow_destructible<T>::value)
{
if (populated) {
content.t = std::move(other);
} else {
new (&content.t) T(std::move(other));
populated = true;
}

return *this;
}

operator bool() const noexcept
{
return populated;
}

bool has_value() const noexcept
{
return populated;
}

const T &operator*() const &noexcept
{
return content.t;
}

T &operator*() &noexcept
{
return content.t;
}

const T &&operator*() const &&noexcept
{
return std::move(content.t);
}

T &&operator*() &&noexcept
{
return std::move(content.t);
}

const T *operator->() const noexcept
{
return &content.t;
}

T *operator->() noexcept
{
return &content.t;
}

void reset() noexcept(std::is_nothrow_destructible<T>::value)
{
if (populated) {
content.t.~T();
populated = false;
}
}

private:
union Content {
T t;
};

bool populated;
Content content;
};

#endif /* _OPTIONAL_H */
13 changes: 9 additions & 4 deletions src/lua/LuaPiGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,10 +912,15 @@ static int l_pigui_button_image_sized(lua_State *l)
int frame_padding = LuaPull<int>(l, 6);
ImColor bg_col = LuaPull<ImColor>(l, 7);
ImColor tint_col = LuaPull<ImColor>(l, 8);
bool res = PiGui::ButtonImageSized(id, size, imgSize, uv0, uv1,
frame_padding, bg_col, tint_col);
LuaPush<bool>(l, res);
return 1;
Optional<const char *> id_suffix = LuaPullOpt<const char *>(l, 9);
auto pushed_hovered_held = PiGui::ButtonImageSized(id, size,
imgSize, uv0, uv1, frame_padding, bg_col, tint_col,
id_suffix);

LuaPush<bool>(l, pushed_hovered_held[0]);
LuaPush<bool>(l, pushed_hovered_held[1]);
LuaPush<bool>(l, pushed_hovered_held[2]);
return 3;
}

static int l_pigui_text_wrapped(lua_State *l)
Expand Down
51 changes: 51 additions & 0 deletions src/lua/LuaPushPull.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <lua.hpp>

#include "Lua.h"
#include "Optional.h"
#include "src/lua.h"
#include <string>
#include <tuple>

Expand All @@ -32,6 +34,47 @@ inline void pi_lua_generic_pull(lua_State *l, int index, std::string &out)
const char *buf = luaL_checklstring(l, index, &len);
std::string(buf, len).swap(out);
}

inline void pi_lua_generic_pull_opt(lua_State *l, int index, Optional<int> &out)
{
int isnum;
lua_Integer d = lua_tointegerx(l, index, &isnum);
if (isnum) {
out = d;
}
}
inline void pi_lua_generic_pull_opt(lua_State *l, int index, Optional<unsigned int> &out)
{
int isnum;
lua_Unsigned d = lua_tounsignedx(l, index, &isnum);
if (isnum) {
out = d;
}
}
template <typename Float, typename = typename std::enable_if<std::is_floating_point<Float>::value>::type>
inline void pi_lua_generic_pull_opt(lua_State *l, int index, Optional<Float> &out)
{
int isnum;
lua_Number d = lua_tonumberx(l, index, &isnum);
if (isnum) {
out = d;
}
}
inline void pi_lua_generic_pull_opt(lua_State *l, int index, Optional<const char *> &out)
{
const char *str = lua_tolstring(l, index, nullptr);
if (str) {
out = str;
}
}
inline void pi_lua_generic_pull_opt(lua_State *l, int index, Optional<std::string> &out)
{
std::size_t len;
const char *str = lua_tolstring(l, index, &len);
if (str) {
out = std::string(str, len);
}
}
template <typename Type>
inline void LuaPush(lua_State *l, Type value)
{
Expand All @@ -46,6 +89,14 @@ inline Type LuaPull(lua_State *l, int index)
return value;
}

template <typename Type>
inline Optional<Type> LuaPullOpt(lua_State *l, int index)
{
Optional<Type> value;
pi_lua_generic_pull_opt(l, index, value);
return value;
}

// Pull a value with an optional default.
template <typename Type>
inline Type LuaPull(lua_State *l, int index, Type defaultVal)
Expand Down
3 changes: 2 additions & 1 deletion src/pigui/PiGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include "FileSystem.h"
#include "Optional.h"
#include "RefCounted.h"
#include "imgui/imgui.h"

Expand Down Expand Up @@ -129,7 +130,7 @@ namespace PiGui {
bool CircularSlider(const ImVec2 &center, float *v, float v_min, float v_max);

bool LowThrustButton(const char *label, const ImVec2 &size_arg, int thrust_level, const ImVec4 &bg_col, int frame_padding, ImColor gauge_fg, ImColor gauge_bg);
bool ButtonImageSized(ImTextureID user_texture_id, const ImVec2 &size, const ImVec2 &imgSize, const ImVec2 &uv0, const ImVec2 &uv1, int frame_padding, const ImVec4 &bg_col, const ImVec4 &tint_col);
std::array<bool, 3> ButtonImageSized(ImTextureID user_texture_id, const ImVec2 &size, const ImVec2 &imgSize, const ImVec2 &uv0, const ImVec2 &uv1, int frame_padding, const ImVec4 &bg_col, const ImVec4 &tint_col, const Optional<const char *> &id_suffix);

void ThrustIndicator(const std::string &id_string, const ImVec2 &size, const ImVec4 &thrust, const ImVec4 &velocity, const ImVec4 &bg_col, int frame_padding, ImColor vel_fg, ImColor vel_bg, ImColor thrust_fg, ImColor thrust_bg);

Expand Down
Loading

0 comments on commit 51656f0

Please sign in to comment.