1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
/***************************************************************************
* scenemanager.c
*
* Copyright 2005 Ian McIntosh
* ian_mcintosh@linuxadvocate.org
****************************************************************************/
/*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
Purpose of scenemanager.c:
- Keep text labels and other screen objects from overlapping
- Prevent the same text from showing up too often (currently not more than once)
*/
#include <gtk/gtk.h>
#include "main.h"
#include "scenemanager.h"
#define ENABLE_NO_DUPLICATE_LABELS
void scenemanager_new(scenemanager_t** ppReturn)
{
// create new scenemanager and return it
scenemanager_t* pNew = g_new0(scenemanager_t, 1);
pNew->pLabelHash = g_hash_table_new(g_str_hash, g_str_equal);
pNew->pTakenRegion = gdk_region_new();
*ppReturn = pNew;
}
// NOTE: must be called before any scenemanager_can_draw_* calls
void scenemanager_set_screen_dimensions(scenemanager_t* pSceneManager, gint nWindowWidth, gint nWindowHeight)
{
pSceneManager->nWindowWidth = nWindowWidth;
pSceneManager->nWindowHeight = nWindowHeight;
}
gboolean scenemanager_can_draw_label_at(scenemanager_t* pSceneManager, const gchar* pszLabel, GdkPoint* unused_pScreenLocation, gint nFlags)
{
#ifdef ENABLE_NO_DUPLICATE_LABELS
g_assert(pSceneManager != NULL);
g_assert(pszLabel != NULL);
// g_assert(pScreenLocation != NULL);
// NOTE: ignore pScreenLocation for now
gpointer pKey, pValue;
// Can draw if it doesn't exist in table
return (FALSE == g_hash_table_lookup_extended(pSceneManager->pLabelHash, pszLabel, &pKey, &pValue));
#else
return TRUE;
#endif
}
gboolean scenemanager_can_draw_polygon(scenemanager_t* pSceneManager, GdkPoint *pPoints, gint nNumPoints, gint nFlags)
{
//
// 1) Enforce on-screen rules
//
if(nFlags & SCENEMANAGER_FLAG_FULLY_ON_SCREEN) {
// all points must be within screen box
gint i;
for(i=0 ; i<nNumPoints ; i++) {
GdkPoint* pPoint = &pPoints[i];
if(pPoint->x < 0 || pPoint->x > pSceneManager->nWindowWidth) return FALSE;
if(pPoint->y < 0 || pPoint->y > pSceneManager->nWindowHeight) return FALSE;
}
// else go on to test below
}
else if(nFlags & SCENEMANAGER_FLAG_PARTLY_ON_SCREEN) {
// one point must be withing screen box XXX: handle polygon bigger than window case?
gint i;
gboolean bFound = FALSE;
for(i=0 ; i<nNumPoints ; i++) {
GdkPoint* pPoint = &pPoints[i];
if(pPoint->x > 0 && pPoint->x < pSceneManager->nWindowWidth) {
bFound = TRUE;
break;
}
if(pPoint->y > 0 && pPoint->y < pSceneManager->nWindowHeight) {
bFound = TRUE;
break;
}
}
if(!bFound) return FALSE;
// else go on to test below
}
//
// 2) Enforce overlap rules
//
GdkRegion* pNewRegion = gdk_region_polygon(pPoints, nNumPoints, GDK_WINDING_RULE);
gdk_region_intersect(pNewRegion, pSceneManager->pTakenRegion); // sets pNewRegion to the intersection of itself and the 'taken region'
gboolean bOK = gdk_region_empty(pNewRegion); // it's ok to draw here if the intersection is empty
gdk_region_destroy(pNewRegion);
return bOK;
}
gboolean scenemanager_can_draw_rectangle(scenemanager_t* pSceneManager, GdkRectangle* pRect, gint nFlags)
{
//
// 1) Enforce on-screen rules
//
if(nFlags & SCENEMANAGER_FLAG_FULLY_ON_SCREEN) {
// basic rect1 contains rect2 test
if((pRect->x) <= 0) return FALSE;
if((pRect->y) <= 0) return FALSE;
if((pRect->x + pRect->width) > pSceneManager->nWindowWidth) return FALSE;
if((pRect->y + pRect->height) > pSceneManager->nWindowHeight) return FALSE;
}
else if(nFlags & SCENEMANAGER_FLAG_PARTLY_ON_SCREEN) {
// basic rect intersect test
if((pRect->x + pRect->width) <= 0) return FALSE;
if((pRect->y + pRect->height) <= 0) return FALSE;
if((pRect->x) > pSceneManager->nWindowWidth) return FALSE;
if((pRect->y) > pSceneManager->nWindowHeight) return FALSE;
}
//
// 2) Enforce overlap rules
//
GdkRegion* pNewRegion = gdk_region_rectangle(pRect);
gdk_region_intersect(pNewRegion, pSceneManager->pTakenRegion); // sets pNewRegion to the intersection of itself and the 'taken region'
gboolean bOK = gdk_region_empty(pNewRegion); // it's ok to draw here if the intersection is empty
gdk_region_destroy(pNewRegion);
return bOK;
}
void scenemanager_claim_label(scenemanager_t* pSceneManager, gchar* pszLabel)
{
#ifdef ENABLE_NO_DUPLICATE_LABELS
g_assert(pSceneManager != NULL);
// Just putting the label into the hash is enough
g_hash_table_insert(pSceneManager->pLabelHash, pszLabel, NULL);
#endif
}
void scenemanager_claim_polygon(scenemanager_t* pSceneManager, GdkPoint *pPoints, gint nNumPoints)
{
// Create a GdkRegion from the given points and union it with the 'taken region'
GdkRegion* pNewRegion = gdk_region_polygon(pPoints, nNumPoints, GDK_WINDING_RULE);
gdk_region_union(pSceneManager->pTakenRegion, pNewRegion);
gdk_region_destroy(pNewRegion);
}
void scenemanager_claim_rectangle(scenemanager_t* pSceneManager, GdkRectangle* pRect)
{
// Add the area of the rectangle to the region
gdk_region_union_with_rect(pSceneManager->pTakenRegion, pRect);
}
void scenemanager_clear(scenemanager_t* pSceneManager)
{
g_assert(pSceneManager != NULL);
// destroy and recreate hash table (XXX: better way to clear it?)
g_hash_table_destroy(pSceneManager->pLabelHash);
pSceneManager->pLabelHash = g_hash_table_new(g_str_hash, g_str_equal);
// Empty the region (XXX: better way?)
gdk_region_destroy(pSceneManager->pTakenRegion);
pSceneManager->pTakenRegion = gdk_region_new();
}
|