Skip to content

Commit

Permalink
reafactor: moved showing tooltips to separated module
Browse files Browse the repository at this point in the history
FossilOrigin-Name: ab1e68a898751a6e9a432192eeee149761dc2693b928839caa55e5e5887fa4a7
  • Loading branch information
thindil committed Nov 1, 2024
1 parent 28990db commit 4eca63a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
7 changes: 1 addition & 6 deletions src/newui/coreui.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ type GameState* = enum
mainMenu, quitGame, news

const
tooltipDelay*: float = 1000.0
## For how long the player hovers the mouse over UI element before a
## tooltip will be shown
dtime*: float = 40.0
## The length in miliseconds of one game's frame
dtime*: float = 40.0 ## The length in miliseconds of one game's frame

var
windowWidth*: Positive = 600 ## The width of the game's main window
windowHeight*: Positive = 400 ## The height of the game's main window
tooltipTime*: float = tooltipDelay ## How long left to show a tooltip
11 changes: 3 additions & 8 deletions src/newui/mainmenu.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import std/[os, sequtils]
import contracts, nuklear/nuklear_sdl_renderer
import ../game
import coreui
import coreui, utilsui

var
logo: PImage = nil
Expand Down Expand Up @@ -65,15 +65,10 @@ proc showMainMenu*(state: var GameState) {.raises: [], tags: [], contractual.} =
w: float = 150
h: float = 40
row(x = x, y = 0, w = w, h = h):
var bounds: NimRect = getWidgetBounds()
let bounds: NimRect = getWidgetBounds()
labelButton(title = "New game"):
echo "button pressed"
if isMouseHovering(bounds):
tooltipTime -= dtime
if tooltipTime <= 0.0:
tooltip("Set and start a new game")
else:
tooltipTime = tooltipDelay
bounds.showTooltip(text = "Set and start a new game")
var y: float = h;
if showLoadButton:
row(x = x, y = y, w = w, h = h):
Expand Down
38 changes: 38 additions & 0 deletions src/newui/utilsui.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2024 Bartek thindil Jasicki
#
# This file is part of Steam Sky.
#
# Steam Sky is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Steam Sky is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Steam Sky. If not, see <http://www.gnu.org/licenses/>.

## Provides code related to the game's UI, like tooltips

import contracts, nuklear/nuklear_sdl_renderer
import coreui

const
tooltipDelay: float = 1000.0
## For how long the player hovers the mouse over UI element before a
## tooltip will be shown

var
tooltipTime: float = tooltipDelay ## How long left to show a tooltip

proc showTooltip*(bounds: NimRect; text: string) {.raises: [], tags: [],
contractual.} =
if isMouseHovering(bounds):
tooltipTime -= dtime
if tooltipTime <= 0.0:
tooltip("Set and start a new game")
else:
tooltipTime = tooltipDelay

0 comments on commit 4eca63a

Please sign in to comment.