Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EAGLView.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eb9dc73d01035b415660be24aa8e3e6623d6816b (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
#import "Common.h"
#import "EAGLView.h"
#import "MapsAppDelegate.h"
#import "LocationManager.h"
#import "MWMDirectionView.h"

#import "../Platform/opengl/iosOGLContextFactory.h"

#include "Framework.h"
#include "indexer/classificator_loader.hpp"

#include "platform/platform.hpp"

#include "drape/visual_scale.hpp"

#include "std/bind.hpp"
#include "std/limits.hpp"
#include "std/unique_ptr.hpp"

@implementation EAGLView

namespace
{
// Returns native scale if it's possible.
double correctContentScale()
{
  UIScreen * uiScreen = [UIScreen mainScreen];
  
  if (isIOS7)
    return [uiScreen respondsToSelector:@selector(scale)] ? [uiScreen scale] : 1.f;
  else
    return [uiScreen respondsToSelector:@selector(nativeScale)] ? [uiScreen nativeScale] : 1.f;
}

// Returns DPI as exact as possible. It works for iPhone, iPad and iWatch.
double getExactDPI(double contentScaleFactor)
{
  float const iPadDPI = 132.f;
  float const iPhoneDPI = 163.f;
  float const mDPI = 160.f;

  switch (UI_USER_INTERFACE_IDIOM())
  {
    case UIUserInterfaceIdiomPhone:
      return iPhoneDPI * contentScaleFactor;
    case UIUserInterfaceIdiomPad:
      return iPadDPI * contentScaleFactor;
    default:
      return mDPI * contentScaleFactor;
  }
}
} //  namespace

// You must implement this method
+ (Class)layerClass
{
  return [CAEAGLLayer class];
}

// The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id)initWithCoder:(NSCoder *)coder
{
  NSLog(@"EAGLView initWithCoder Started");
  self = [super initWithCoder:coder];
  if (self && !MapsAppDelegate.theApp.isDaemonMode)
    [self initialize];

  NSLog(@"EAGLView initWithCoder Ended");
  return self;
}

- (void)initialize
{
  lastViewSize = CGRectZero;
  _widgetsManager = [[MWMMapWidgets alloc] init];

  // Setup Layer Properties
  CAEAGLLayer * eaglLayer = (CAEAGLLayer *)self.layer;

  eaglLayer.opaque = YES;
  eaglLayer.drawableProperties = @{kEAGLDrawablePropertyRetainedBacking : @NO,
                                   kEAGLDrawablePropertyColorFormat : kEAGLColorFormatRGBA8};

  // Correct retina display support in opengl renderbuffer
  self.contentScaleFactor = correctContentScale();

  m_factory = make_unique_dp<dp::ThreadSafeFactory>(new iosOGLContextFactory(eaglLayer));
}

- (void)createDrapeEngineWithWidth:(int)width height:(int)height
{
  NSLog(@"EAGLView createDrapeEngine Started");
  if (MapsAppDelegate.theApp.isDaemonMode)
    return;

  Framework::DrapeCreationParams p;
  p.m_surfaceWidth = width;
  p.m_surfaceHeight = height;
  p.m_visualScale = dp::VisualScale(getExactDPI(self.contentScaleFactor));

  [self.widgetsManager setupWidgets:p];
  GetFramework().CreateDrapeEngine(make_ref<dp::OGLContextFactory>(m_factory), move(p));

  _drapeEngineCreated = YES;

  NSLog(@"EAGLView createDrapeEngine Ended");
}

- (void)addSubview:(UIView *)view
{
  [super addSubview:view];
  for (UIView * v in self.subviews)
  {
    if ([v isKindOfClass:[MWMDirectionView class]])
    {
      [self bringSubviewToFront:v];
      break;
    }
  }
}

- (void)applyOnSize:(int)width withHeight:(int)height
{
  dispatch_async(dispatch_get_main_queue(), ^
  {
    GetFramework().OnSize(width, height);
    [self.widgetsManager resize:CGSizeMake(width, height)];
  });
}

- (void)onSize:(int)width withHeight:(int)height
{
  int w = width * self.contentScaleFactor;
  int h = height * self.contentScaleFactor;

  if (GetFramework().GetDrapeEngine() == nullptr)
  {
    [self createDrapeEngineWithWidth:w height:h];
    return;
  }

  [self applyOnSize:w withHeight:h];
}

- (void)layoutSubviews
{
  if (!CGRectEqualToRect(lastViewSize, self.frame))
  {
    lastViewSize = self.frame;
    CGSize const s = self.bounds.size;
    [self onSize:s.width withHeight:s.height];
  }
  [super layoutSubviews];
}

- (void)deallocateNative
{
  GetFramework().PrepareToShutdown();
  m_factory.reset();
}

- (CGPoint)viewPoint2GlobalPoint:(CGPoint)pt
{
  CGFloat const scaleFactor = self.contentScaleFactor;
  m2::PointD const ptG = GetFramework().PtoG(m2::PointD(pt.x * scaleFactor, pt.y * scaleFactor));
  return CGPointMake(ptG.x, ptG.y);
}

- (CGPoint)globalPoint2ViewPoint:(CGPoint)pt
{
  CGFloat const scaleFactor = self.contentScaleFactor;
  m2::PointD const ptP = GetFramework().GtoP(m2::PointD(pt.x, pt.y));
  return CGPointMake(ptP.x / scaleFactor, ptP.y / scaleFactor);
}

- (void)setPresentAvailable:(BOOL)available
{
  static_cast<iosOGLContextFactory*>(m_factory.get())->setPresentAvailable(available);
}

@end