1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * Copyright (c) 2005-2013 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This software was released into the Public Domain. As with most public |
---|
7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
8 | * Jonathan Clark, or by Sam Hocevar. |
---|
9 | */ |
---|
10 | |
---|
11 | #if HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <string.h> |
---|
16 | |
---|
17 | #include "common.h" |
---|
18 | |
---|
19 | #include "imlib/input.h" |
---|
20 | #include "imlib/status.h" |
---|
21 | #include "imlib/guistat.h" |
---|
22 | |
---|
23 | class gui_status_node |
---|
24 | { |
---|
25 | public : |
---|
26 | char *name; |
---|
27 | gui_status_node *next; |
---|
28 | AVisualObject *show; |
---|
29 | AWindow *stat_win; |
---|
30 | int last_update; |
---|
31 | Timer last_time; |
---|
32 | gui_status_node(char const *Name, AVisualObject *Show, gui_status_node *Next) |
---|
33 | { name = strdup(Name); |
---|
34 | show=Show; |
---|
35 | next=Next; |
---|
36 | last_update=0; |
---|
37 | stat_win=NULL; |
---|
38 | } |
---|
39 | ~gui_status_node(); |
---|
40 | } ; |
---|
41 | |
---|
42 | |
---|
43 | gui_status_node::~gui_status_node() |
---|
44 | { |
---|
45 | free(name); |
---|
46 | if (show) |
---|
47 | delete show; |
---|
48 | if (stat_win) |
---|
49 | { |
---|
50 | wm->close_window(stat_win); |
---|
51 | wm->flush_screen(); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | void gui_status_manager::draw_bar(gui_status_node *whom, int perc) |
---|
56 | { |
---|
57 | long l = whom->stat_win->x2() - whom->stat_win->x1() - 6; |
---|
58 | long h = wm->font()->Size().y; |
---|
59 | |
---|
60 | whom->stat_win->m_surf->Bar(ivec2(whom->stat_win->x1() + 1, |
---|
61 | whom->stat_win->y2() - h - 1), |
---|
62 | ivec2(whom->stat_win->x2() - 1, |
---|
63 | whom->stat_win->y2() - 1), |
---|
64 | wm->black()); |
---|
65 | whom->stat_win->m_surf->Bar(ivec2(whom->stat_win->x1() + 2, |
---|
66 | whom->stat_win->y2() - h), |
---|
67 | ivec2(whom->stat_win->x2() - 2, |
---|
68 | whom->stat_win->y2() - 2), |
---|
69 | wm->dark_color()); |
---|
70 | if (perc) |
---|
71 | whom->stat_win->m_surf->Bar(ivec2(whom->stat_win->x1() + 3, |
---|
72 | whom->stat_win->y2() - h + 1), |
---|
73 | ivec2(whom->stat_win->x1() + l * perc / 100, |
---|
74 | whom->stat_win->y2() - 3), |
---|
75 | wm->bright_color()); |
---|
76 | } |
---|
77 | |
---|
78 | void gui_status_manager::push(char const *name, AVisualObject *show) |
---|
79 | { |
---|
80 | first=new gui_status_node(name,show,first); |
---|
81 | } |
---|
82 | |
---|
83 | gui_status_manager::gui_status_manager() |
---|
84 | { |
---|
85 | first=NULL; |
---|
86 | strcpy(title,"STATUS"); |
---|
87 | last_perc=0; |
---|
88 | } |
---|
89 | |
---|
90 | void gui_status_manager::update(int percentage) |
---|
91 | { |
---|
92 | last_perc=percentage; |
---|
93 | if (first) |
---|
94 | { |
---|
95 | if (!first->stat_win) |
---|
96 | { |
---|
97 | if (first->last_time.Poll() > 1.0) |
---|
98 | { |
---|
99 | long wx=xres/2,wy=10,len1=strlen(first->name)*wm->font()->Size().x+10,len2=0,len3, |
---|
100 | h1=wm->font()->Size().y+5,h2=first->show ? first->show->Size().y : 0; |
---|
101 | |
---|
102 | if (first->show) len2=first->show->Size().x/2; |
---|
103 | if (len2>len1) len3=len2; else len3=len1; |
---|
104 | wx-=len3/2; |
---|
105 | |
---|
106 | |
---|
107 | gui_status_node *p=first->next; |
---|
108 | while (p && !p->stat_win) p=p->next; |
---|
109 | if (p) wy=p->stat_win->m_pos.y+p->stat_win->y2()+5; |
---|
110 | |
---|
111 | int mx = first->stat_win->x1() + 1; |
---|
112 | int my = first->stat_win->y1() + wm->font()->Size().y / 2; |
---|
113 | first->stat_win = wm->CreateWindow(ivec2(wx, wy), ivec2(len3, h1*2+h2), "status"); |
---|
114 | wm->font()->PutString(first->stat_win->m_surf, ivec2(mx, my), first->name, wm->black()); |
---|
115 | wm->font()->PutString(first->stat_win->m_surf, ivec2(mx, my), first->name, wm->bright_color()); |
---|
116 | if (first->show) |
---|
117 | first->show->Draw(first->stat_win->m_surf, ivec2((first->stat_win->x2() - first->stat_win->x1()) / 2 - first->show->Size().x / 2, my + h1), nullptr); |
---|
118 | |
---|
119 | draw_bar(first,percentage); |
---|
120 | wm->flush_screen(); |
---|
121 | } |
---|
122 | } else |
---|
123 | { |
---|
124 | if (percentage>first->last_update) |
---|
125 | { |
---|
126 | first->last_update=percentage; |
---|
127 | draw_bar(first,percentage); |
---|
128 | wm->flush_screen(); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | void gui_status_manager::force_display() |
---|
135 | { |
---|
136 | update(last_perc); |
---|
137 | } |
---|
138 | |
---|
139 | void gui_status_manager::pop() |
---|
140 | { |
---|
141 | ASSERT(first, "no status to pop!"); |
---|
142 | |
---|
143 | gui_status_node *p = first; |
---|
144 | first = first->next; |
---|
145 | delete p; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | |
---|