summaryrefslogtreecommitdiff
path: root/clutter/clutter/clutter-pick-context.c
blob: 2f054d318fc3bb345d2d26112d9d800382508e82 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (C) 2019 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "clutter-build-config.h"

#include "clutter-backend.h"
#include "clutter-pick-context-private.h"

struct _ClutterPickContext
{
  grefcount ref_count;

  ClutterPickMode mode;
  ClutterPickStack *pick_stack;

  graphene_ray_t ray;
  graphene_point3d_t point;
};

G_DEFINE_BOXED_TYPE (ClutterPickContext, clutter_pick_context,
                     clutter_pick_context_ref,
                     clutter_pick_context_unref)

ClutterPickContext *
clutter_pick_context_new_for_view (ClutterStageView         *view,
                                   ClutterPickMode           mode,
                                   const graphene_point3d_t *point,
                                   const graphene_ray_t     *ray)
{
  ClutterPickContext *pick_context;
  CoglContext *context;

  pick_context = g_new0 (ClutterPickContext, 1);
  g_ref_count_init (&pick_context->ref_count);
  pick_context->mode = mode;
  graphene_ray_init_from_ray (&pick_context->ray, ray);
  graphene_point3d_init_from_point (&pick_context->point, point);

  context = clutter_backend_get_cogl_context (clutter_get_default_backend ());
  pick_context->pick_stack = clutter_pick_stack_new (context);

  return pick_context;
}

ClutterPickContext *
clutter_pick_context_ref (ClutterPickContext *pick_context)
{
  g_ref_count_inc (&pick_context->ref_count);
  return pick_context;
}

static void
clutter_pick_context_dispose (ClutterPickContext *pick_context)
{
  g_clear_pointer (&pick_context->pick_stack, clutter_pick_stack_unref);
}

void
clutter_pick_context_unref (ClutterPickContext *pick_context)
{
  if (g_ref_count_dec (&pick_context->ref_count))
    {
      clutter_pick_context_dispose (pick_context);
      g_free (pick_context);
    }
}

void
clutter_pick_context_destroy (ClutterPickContext *pick_context)
{
  clutter_pick_context_dispose (pick_context);
  clutter_pick_context_unref (pick_context);
}

/**
 * clutter_pick_context_get_mode: (skip)
 */
ClutterPickMode
clutter_pick_context_get_mode (ClutterPickContext *pick_context)
{
  return pick_context->mode;
}

ClutterPickStack *
clutter_pick_context_steal_stack (ClutterPickContext *pick_context)
{
  clutter_pick_stack_seal (pick_context->pick_stack);
  return g_steal_pointer (&pick_context->pick_stack);
}

/**
 * clutter_pick_context_log_pick:
 * @pick_context: a #ClutterPickContext
 * @box: a #ClutterActorBox
 * @actor: a #ClutterActor
 *
 * Logs a pick rectangle into the pick stack.
 */
void
clutter_pick_context_log_pick (ClutterPickContext    *pick_context,
                               const ClutterActorBox *box,
                               ClutterActor          *actor)
{
  clutter_pick_stack_log_pick (pick_context->pick_stack, box, actor);
}

/**
 * clutter_pick_context_push_clip:
 * @pick_context: a #ClutterPickContext
 * @box: a #ClutterActorBox
 *
 * Pushes a clip rectangle defined by @box into the pick stack. Pop with
 * clutter_pick_context_pop_clip() when done.
 */
void
clutter_pick_context_push_clip (ClutterPickContext    *pick_context,
                                const ClutterActorBox *box)
{
  clutter_pick_stack_push_clip (pick_context->pick_stack, box);
}

/**
 * clutter_pick_context_pop_clip:
 * @pick_context: a #ClutterPickContext
 *
 * Pops the current clip rectangle from the clip stack. It is a programming
 * error to call this without a corresponding clutter_pick_context_push_clip()
 * call first.
 */
void
clutter_pick_context_pop_clip (ClutterPickContext *pick_context)
{
  clutter_pick_stack_pop_clip (pick_context->pick_stack);
}

/**
 * clutter_pick_context_push_transform:
 * @pick_context: a #ClutterPickContext
 * @transform: a #graphene_matrix_t
 *
 * Pushes @transform into the pick stack. Pop with
 * clutter_pick_context_pop_transform() when done.
 */
void
clutter_pick_context_push_transform (ClutterPickContext      *pick_context,
                                     const graphene_matrix_t *transform)
{
  clutter_pick_stack_push_transform (pick_context->pick_stack, transform);
}

/**
 * clutter_pick_context_get_transform:
 * @pick_context: a #ClutterPickContext
 * @out_matrix: (out): a #graphene_matrix_t
 *
 * Retrieves the current transform of the pick stack.
 */
void
clutter_pick_context_get_transform (ClutterPickContext *pick_context,
                                    graphene_matrix_t  *out_transform)
{
  clutter_pick_stack_get_transform (pick_context->pick_stack, out_transform);
}

/**
 * clutter_pick_context_pop_transform:
 * @pick_context: a #ClutterPickContext
 *
 * Pops the current transform from the clip stack. It is a programming error
 * to call this without a corresponding clutter_pick_context_push_transform()
 * call first.
 */
void
clutter_pick_context_pop_transform (ClutterPickContext *pick_context)
{
  clutter_pick_stack_pop_transform (pick_context->pick_stack);
}

gboolean
clutter_pick_context_intersects_box (ClutterPickContext   *pick_context,
                                     const graphene_box_t *box)
{
  return graphene_box_contains_point (box, &pick_context->point) ||
         graphene_ray_intersects_box (&pick_context->ray, box);
}