Skip to content

Commit

Permalink
Merge pull request #1 from grimmier378/themez_support
Browse files Browse the repository at this point in the history
style:
  • Loading branch information
yb-f committed Apr 20, 2024
2 parents 8761dc7 + 06c3761 commit 307547f
Show file tree
Hide file tree
Showing 3 changed files with 3,449 additions and 2 deletions.
71 changes: 69 additions & 2 deletions init.lua
@@ -1,5 +1,7 @@
local mq = require('mq')
local ImGui = require 'ImGui'
local Icons = require('mq.ICONS')
local gIcon = Icons.MD_SETTINGS

local spawns = {}
local running = true
Expand All @@ -11,8 +13,14 @@ local openGUI, drawGUI = true, true
local angle = 0
local size = 25
local column_count = 9
local script = 'MobList'
local direction_arrow = false

local LoadTheme = require('lib.theme_loader')
local defaults = require('lib.themes')
local themeFile = string.format('%s/MyThemeZ.lua', mq.configDir)
local configFile = string.format('%s/moblist.lua', mq.configDir)
local themeName = 'Default'
local theme, settings, defaultSettings = {}, {}, {}
local mobheader = "\ay[\agMob List\ay]"

local updated_data = false
Expand All @@ -34,6 +42,14 @@ local filter = {
['class_reverse'] = false
}

defaultSettings = {
[script] = {
Scale = 1.0,
LoadTheme = 'Default',
locked = false,
},
}

local ColumnID_ID = 0
local ColumnID_Lvl = 1
local ColumnID_DisplayName = 2
Expand All @@ -44,7 +60,7 @@ local ColumnID_Body = 6
local ColumnID_Race = 7
local ColumnID_Class = 8
local ColumnID_Direction = 9

local themeID = 1
function RotatePoint(p, cx, cy, angle)
local radians = math.rad(angle)
local cosA = math.cos(radians)
Expand Down Expand Up @@ -75,6 +91,38 @@ function DrawArrow(topPoint, width, height, color)
draw_list:AddTriangleFilled(p1, p2, p3, ImGui.GetColorU32(color))
end

---comment Check to see if the file we want to work on exists.
---@param name string -- Full Path to file
---@return boolean -- returns true if the file exists and false otherwise
local function File_Exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end

local function loadThemeSettings()
if not File_Exists(configFile) then
mq.pickle(configFile, defaultSettings)
loadThemeSettings()
else
settings = dofile(configFile)
end
if not File_Exists(themeFile) then
mq.pickle(themeFile, defaults)
loadThemeSettings()
else
theme = dofile(themeFile)
end
if settings[script].LoadTheme == nil then
settings[script].LoadTheme = themeName
end
themeName = settings[script].LoadTheme or themeName
for tID, tData in pairs(theme.Theme) do
if tData['Name'] == themeName then
themeID = tID
end
end
end

local function isType(spawn)
return spawn.Type() == filter.Type[filter.Type_Selected]
end
Expand Down Expand Up @@ -114,6 +162,7 @@ end

local function main()
create_spawn_list()
loadThemeSettings()
while running == true do
mq.delay('1s')
create_spawn_list()
Expand Down Expand Up @@ -203,8 +252,25 @@ end

local function displayGUI()
if not openGUI then running = false end
local ColorCount, StyleCount = LoadTheme.StartTheme(theme.Theme[themeID])
openGUI, drawGUI = ImGui.Begin("Mob List##" .. myName, openGUI, window_flags)
if drawGUI and not mq.TLO.Me.Zoning() then
if ImGui.Button(gIcon..'##PlayerTarg') then end
if ImGui.BeginPopupContextWindow() then
if ImGui.BeginMenu('ThemeZ') then
for k, data in pairs(theme.Theme) do
if ImGui.MenuItem(data.Name, '', (data.Name == themeName)) then
themeName = data.Name
themeID = k
settings[script].LoadTheme = themeName
mq.pickle(configFile, settings)
end
end
ImGui.EndMenu()
end
ImGui.EndPopup()
end
ImGui.SameLine()
ImGui.Text("Level Range")
ImGui.SameLine()
ImGui.PushItemWidth(45)
Expand Down Expand Up @@ -406,6 +472,7 @@ local function displayGUI()
ImGui.EndTable()
end
end
LoadTheme.EndTheme(ColorCount, StyleCount)
ImGui.End()
end

Expand Down
128 changes: 128 additions & 0 deletions lib/theme_loader.lua
@@ -0,0 +1,128 @@
local mq = require('mq')
local ImGui = require('ImGui')

local LoadTheme = {}

function LoadTheme.shallowcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end

function LoadTheme.PCallString(str)
local func, err = load(str)
if not func then
print(err)
return false, err
end

return pcall(func)
end

function LoadTheme.EvaluateLua(str)
local runEnv = [[mq = require('mq')
%s
]]
return LoadTheme.PCallString(string.format(runEnv, str))
end


---@param theme table
---@return integer, integer
function LoadTheme.StartTheme(theme)
if not theme == type('table') then return 0,0 end

local themeColorPop = 0
local themeStylePop = 0

if theme ~= nil then
for n, t in pairs(theme) do
if t.color then
ImGui.PushStyleColor(ImGuiCol[t.element], t.color.r, t.color.g, t.color.b, t.color.a)
themeColorPop = themeColorPop + 1
elseif t.stylevar then
ImGui.PushStyleVar(ImGuiStyleVar[t.stylevar], t.value)
themeStylePop = themeStylePop + 1
else
if type(t) == 'table' then
if t['Dynamic_Color'] then
local ret, colors = LoadTheme.EvaluateLua(t['Dynamic_Color'])
if ret then
---@diagnostic disable-next-line: param-type-mismatch
ImGui.PushStyleColor(ImGuiCol[n], colors)
themeColorPop = themeColorPop + 1
end
elseif t['Dynamic_Var'] then
local ret, var = LoadTheme.EvaluateLua(t['Dynamic_Var'])
if ret then
if type(var) == 'table' then
---@diagnostic disable-next-line: param-type-mismatch, deprecated
ImGui.PushStyleVar(ImGuiStyleVar[n], unpack(var))
else
---@diagnostic disable-next-line: param-type-mismatch
ImGui.PushStyleVar(ImGuiStyleVar[n], var)
end
themeStylePop = themeStylePop + 1
end
elseif n == 'Color' then
for cID, cData in pairs(t) do
ImGui.PushStyleColor(cID, ImVec4(cData.Color[1], cData.Color[2], cData.Color[3], cData.Color[4]))
themeColorPop = themeColorPop + 1
end
elseif n == 'Style' then
for sID, sData in pairs (t) do
if sData.Size ~= nil then
ImGui.PushStyleVar(sID, sData.Size)
themeStylePop = themeStylePop + 1
elseif sData.X ~= nil then
ImGui.PushStyleVar(sID, sData.X, sData.Y)
themeStylePop = themeStylePop + 1
end
end
elseif #t == 4 then
local colors = LoadTheme.shallowcopy(t)
for i = 1, 4 do
if type(colors[i]) == 'string' then
local ret, color = LoadTheme.EvaluateLua(colors[i])
if ret then
colors[i] = color
end
end
end
---@diagnostic disable-next-line: param-type-mismatch, deprecated
ImGui.PushStyleColor(ImGuiCol[n], unpack(colors))
themeColorPop = themeColorPop + 1
else
---@diagnostic disable-next-line: param-type-mismatch, deprecated
-- printf("Var %s \t unpack %s", n, unpack(t))
ImGui.PushStyleVar(ImGuiStyleVar[n], unpack(t))
themeStylePop = themeStylePop + 1
end
end
end
end
end

return themeColorPop, themeStylePop
end

---@param themeColorPop integer
---@param themeStylePop integer
function LoadTheme.EndTheme(themeColorPop, themeStylePop)
if themeColorPop > 0 then
ImGui.PopStyleColor(themeColorPop)
end
if themeStylePop > 0 then
ImGui.PopStyleVar(themeStylePop)
end
end

return LoadTheme

0 comments on commit 307547f

Please sign in to comment.